Networking Forums

Networking Forums > Computer Networking > Linux Networking > Recvfrom Returns Empty Buffer

Reply
Thread Tools Display Modes

Recvfrom Returns Empty Buffer

 
 
William
Guest
Posts: n/a

 
      03-23-2005, 03:18 AM
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

 
Reply With Quote
 
 
 
 
Raghu Uppalli
Guest
Posts: n/a

 
      03-23-2005, 06:47 PM
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

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python recvfrom not working when on different ip ambu.sreedharan Linux Networking 1 06-16-2011 05:52 PM
Socket: recvfrom sathya Linux Networking 1 05-07-2009 01:08 PM
recvfrom() unblocking.. how to do it? manu Linux Networking 4 04-21-2006 08:12 PM
problem with recvfrom Omega Linux Networking 0 11-21-2005 08:17 PM
How to set Max UDP Receive Buffer Size and Max UDP Transmit Buffer Size under Windows 2000 and Windows 2003 ? msnews.microsoft.com Windows Networking 0 09-14-2005 07:20 PM



1 2 3 4 5 6 7 8 9 10 11