|
||||||||
|
|
#1
|
|
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§ion=2 William |
|
#2
|
|||
|
|||
|
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 ![]() |
![]() |
| Tags |
| buffer, empty, recvfrom, returns |
| Thread Tools | |
| Display Modes | |
|
|