Networking Forums  

Go Back   Networking Forums > Networking Newsgroups > Linux Networking

Recvfrom Returns Empty Buffer

Reply
 
Thread Tools Display Modes
  #1  
Old 03-23-2005, 04:18 AM
Default Recvfrom Returns Empty Buffer



I have declared the following struct using C++:
struct PropagateInfo {
string type; // "registration", "gossip", "termination"
int initiatePeerID;
};

which I used as follows:

PropagateInfo* initiatePeerInfo = new PropagateInfo;
initiatePeerInfo->type = type; // type is set to "registration"
initiatePeerInfo->initiatePeerID = ID;

cout << "type before sending to overseer: " << initiatePeerInfo->type << \
endl; // prints "registration"

// sending the struct initatePeerInfo to the server
if ( (numBytes = sendto(sockfd, (void*)initiatePeerInfo, \
MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&serv_addr, sizeof(struct \
sockaddr))) == -1 ) {
cerr << "send to: " << strerror(errno) << endl;
exit(1);
}
cout << "bytes sent: " << numBytes << endl; // prints 512


/**** server side *****/
numBytes = recvfrom( sockfd, initiatePeer, MAX_UDP_PACKET_SIZE, 0, (struct
sockaddr *) &peer_addr, &addrLen );
// I have also tried sizeof(struct PropagateInfo) instead of
MAX_UDP_PACKET_SIZE

cout << "num bytes received: " << numBytes << endl; // prints 512
initiatePeerID = ((PropagateInfo*)initiatePeer)->initiatePeerID;
cout << "initiatePeerID: " << initiatePeerID << endl; // prints the
correct ID

type = ((PropagateInfo*)initiatePeer)->type;
// error - nothing (i.e. "") is printed!!!
cout << "type after receiving: " << type << endl;

output:
type after receiving:

my question:
Why is ((PropagateInfo*)initiatePeer)->type "registration" before sendto;
but "" after recvfrom?!

Thanks for your help.

Documentation:
http://linux.com.hk/PenguinWeb/manpa...send&section=2



William
Reply With Quote
  #2  
Old 03-23-2005, 07:47 PM
Raghu Uppalli
Guest
 
Posts: n/a
Default Re: Recvfrom Returns Empty Buffer

The problem here is that before putting it on the socket, the data is
being marshalled properly.

William wrote:
> I have declared the following struct using C++:
> struct PropagateInfo {
> string type; // "registration", "gossip", "termination"
> int initiatePeerID;
> };
>


Am not sure how string "type" is marshalled here. Usual way to do this
is to define type to be of some simple type (char, char array, int etc)
In fact, in your case, it can be an "short" (well, maybe an int for
4-byte alignment) since you have only 3 possibilities.

> which I used as follows:
>
> PropagateInfo* initiatePeerInfo = new PropagateInfo;
> initiatePeerInfo->type = type; // type is set to "registration"
> initiatePeerInfo->initiatePeerID = ID;
>
> cout << "type before sending to overseer: " << initiatePeerInfo->type

<< \
> endl; // prints "registration"
>
> // sending the struct initatePeerInfo to the server
> if ( (numBytes = sendto(sockfd, (void*)initiatePeerInfo, \
> MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&serv_addr, sizeof(struct

\
> sockaddr))) == -1 ) {
> cerr << "send to: " << strerror(errno) << endl;
> exit(1);
> }
> cout << "bytes sent: " << numBytes << endl; // prints 512


You do not want to send MAX_UDP_PACKET_SIZE here, you just need to send
the sizeof() whatever you are sending. If you see sizeof() your object
it will not be correct 'coz of the string in there.

> /**** server side *****/
> numBytes = recvfrom( sockfd, initiatePeer, MAX_UDP_PACKET_SIZE, 0,

(struct
> sockaddr *) &peer_addr, &addrLen );
> // I have also tried sizeof(struct PropagateInfo) instead of
> MAX_UDP_PACKET_SIZE
> cout << "num bytes received: " << numBytes << endl; // prints 512
> initiatePeerID = ((PropagateInfo*)initiatePeer)->initiatePeerID;
> cout << "initiatePeerID: " << initiatePeerID << endl; // prints the
> correct ID


You are casting a void * to a struct of "unkown" length. Doesn't make
sense. I think you are just lucky that the correct ID is printed.

> type = ((PropagateInfo*)initiatePeer)->type;
> // error - nothing (i.e. "") is printed!!!
> cout << "type after receiving: " << type << endl;
>
> output:
> type after receiving:
>
> my question:
> Why is ((PropagateInfo*)initiatePeer)->type "registration" before

sendto;
> but "" after recvfrom?!


Remove the string and replace with a known length variable and you
should be set.

I'm pretty sure my solution is right, but the explanation needs more
expert comments. I always welcome those

Reply With Quote
Reply

Tags
buffer, empty, recvfrom, returns

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 07:39 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.