Author Topic: NetSend discussion (Post your NetSend questions here)  (Read 42785 times)

racingsolution

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
NetSend discussion (Post your NetSend questions here)
« on: April 18, 2012, 09:15:21 am »
I figured it would be good to pool the NetSend questions into one thread... that way newbies can find lots of info in here.

racingsolution

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: NetSend discussion (Post your NetSend questions here)
« Reply #1 on: April 19, 2012, 12:20:16 pm »
When receiving NetSend responses, you will need to specify the end of the string.  the OnResponse tells you the length of the response so the code below will add a "\0" at the end of the string.  If you don't, you will see some garble after the response

(code by martol1ni)

Code: [Select]
int OnResponse(int event, char *response, int length)
{
// Display as much of the server response as possible.
char buf[500]; // or the maxlength of your response;
    for (int i=0; i<length; i++)
        buf[i] = response[i];
    buf[length] = '\0';
// now finish your action
}
« Last Edit: April 20, 2012, 02:40:35 pm by racingsolution »

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: NetSend discussion (Post your NetSend questions here)
« Reply #2 on: April 19, 2012, 03:28:28 pm »
When receiving NetSend responses, you will need to specify the end of the string.  the OnResponse tells you the length of the response so the code below will add a "\0" at the end of the string.  If you don't, you will see some garble after the response

(code by martol1ni)

int onResponse (int event, char *response, int length) {
    char buf[500]; // or the maxlength of your response;
    for (int i=0; i<length; i++)
        buf = response;
    buf[length] = '\0';
    // Now use buf to finish your action

Even easier:

Code: [Select]
int OnResponse(int event, char *response, int length)
{
    response[length]=0;
    ...
    return(length);
}

Since anything up to the byte at length is a meaningful part of your response, all you need to do is to slam a NULL character at position length and be done with it...
Tim Sommers
DragonFireSDK Team

racingsolution

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: NetSend discussion (Post your NetSend questions here)
« Reply #3 on: April 19, 2012, 11:05:09 pm »
Hey Tim.. so martol1ni had the same code.. except in your example, you have return(length) as opposed to return event; ..   Using response[length] = 0 was throwing that HEAP error on me.. using the new code martol1ni provided, it didn't..

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: NetSend discussion (Post your NetSend questions here)
« Reply #4 on: April 20, 2012, 01:59:07 pm »
The OnResponse() function *must* return some integer.  It can be any value, since the result of this function isn't used currently, but it should probably be non-zero, assuming the function was successful.

The heap error should not be happening, since the memory pointed to by *response should not be corrupted.  Did you change any project settings or otherwise do anything to that memory location, by chance?

Also, if you take a look at this code:

Code: [Select]
int onResponse (int event, char *response, int length) {
    char buf[500]; // or the maxlength of your response;
    for (int i=0; i<length; i++)
        buf = response;
    buf[length] = '\0';
    // Now use buf to finish your action

Looking at your code line by line, you're declaring a new character buffer 500 bytes long.  The for loop, however, doesn't make sense.  You're setting the same pointer over and over (from 0 to length-1).  If you wanted to get at each character in the string, you'd need to write your for loop like so:

Code: [Select]
for (int i=0;i<length;i++)
    buf[i]=response[i];

But, this is unnecessary.  If you're going to do this this way, just leave off the for loop and do a

Code: [Select]
buf=response;
Next, consider what happens if length is greater than 500, or whatever size buffer you declared?  You will overflow your buffer and a crash will follow.

This is why my simple solution calls for slamming a NULL character into the byte at length.  Since, by definition, the response buffer will be at least that long...

I hope this helps!
Tim Sommers
DragonFireSDK Team

racingsolution

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: NetSend discussion (Post your NetSend questions here)
« Reply #5 on: April 20, 2012, 02:37:29 pm »
Hey Tim.. didn't even notice the loop error.. I just realized the reason for the incorrect code was because of the BBCODE on the forum... I put it inside of the CODE brackets so it looks proper. But in regards to the heap error, no I didn't change anything which is why I'm not sure why the easier method of adding the 0 doesn't work. 
« Last Edit: April 20, 2012, 02:40:03 pm by racingsolution »

martol1ni

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #6 on: April 20, 2012, 04:47:47 pm »
I've had the same error when using response[length] = 0, I can't really figure out why...

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: NetSend discussion (Post your NetSend questions here)
« Reply #7 on: April 20, 2012, 06:26:12 pm »
Hey guys, out of curiosity, what version of Visual C++ are you using?
Tim Sommers
DragonFireSDK Team

martol1ni

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #8 on: April 20, 2012, 06:35:27 pm »
Hey guys, out of curiosity, what version of Visual C++ are you using?

2010 :)

We should be able to use codeblocks ;p

racingsolution

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: NetSend discussion (Post your NetSend questions here)
« Reply #9 on: April 20, 2012, 06:41:38 pm »
Hey guys, out of curiosity, what version of Visual C++ are you using?

2010 express

xabaras

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #10 on: April 28, 2012, 11:43:33 am »
Hi, i'd like to retrieve the html code of some pages by NetSend and parse it with some regexps... but even if my pages are quite simple, netsend doesn't put on response the whole content of the page.... and in most of cases, "length" of response is max 15-16000... is there some limit for response of netsend?

nednones

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #11 on: May 04, 2012, 02:02:47 am »
On the subject, does anyone have know what the nest way is to respond to a NetSend() request. I've got a server running php which acknowledges when a query via netsend is made by adding fields to the URL address and logging these down on a file on my server.

The problem is, you can't seem to create a TCP connection since the iOS does not act as a client. I've been testing the netsend function and it literally scans the website and returns back a char[length] of data, e.g. if you don't print anything on your website you'll get a pretty empty char[].

What I want to do is process data sent from the iphone (done this) use my server to then send this data to another sever, which will recieve data and then send this data back to the phone. My problem is the OnResponse function called by NetSend() would have already excecuted. Does anyone have any ideas on how to get round this??

ddub87

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #12 on: May 04, 2012, 06:14:40 am »
My question is simple. What is the best way to parse a netsend response? Like if my php file echos a list of phrases separated by colons, how do i store them in an array? Probably basic, but I can't figure it out

martol1ni

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #13 on: May 04, 2012, 06:52:53 am »
My question is simple. What is the best way to parse a netsend response? Like if my php file echos a list of phrases separated by colons, how do i store them in an array? Probably basic, but I can't figure it out

Lets say you are formatting your output so that it echoes "element1,element2,element3,element4..." separated by commas, you could simly use this little code snippet:

Code: [Select]
#include <vector>
#include <string>

int OnResponse(int event, char *response, int length) {
    std::vector<string> responseArray;
    std::string stringElem = "";
    for (int i=0; i<length; i++) {
        if (response[i] != ",")
            stringElem += response[i]; // start making your element, stopping on ','
        else {
            responseArray.push_back(stringElem);
            a = "";
        }
    }
}
           

I didn't try it with a compilator, but I assume that would work. You can format your response just as you like.

martol1ni

martol1ni

  • Guest
Re: NetSend discussion (Post your NetSend questions here)
« Reply #14 on: May 04, 2012, 06:56:03 am »
On the subject, does anyone have know what the nest way is to respond to a NetSend() request. I've got a server running php which acknowledges when a query via netsend is made by adding fields to the URL address and logging these down on a file on my server.

The problem is, you can't seem to create a TCP connection since the iOS does not act as a client. I've been testing the netsend function and it literally scans the website and returns back a char[length] of data, e.g. if you don't print anything on your website you'll get a pretty empty char[].

What I want to do is process data sent from the iphone (done this) use my server to then send this data to another sever, which will recieve data and then send this data back to the phone. My problem is the OnResponse function called by NetSend() would have already excecuted. Does anyone have any ideas on how to get round this??

With the limited functions on the NetSend command, as no post-functions and so on, I would suggest doing as much as you are capable of in php on your server. If that was my problem, I would just send the NetSend request to the first server, then send a new request from the first server to the second server, dealing with the response when it's back on my first server, and then return it to the device. That is at least how I am deling with multiple queries. :)

martol1ni