c++ - Trouble with packing / unpacking ints to and from char* -
I have the following code, which transfers four * for a receiver in an UDP socket: < Pre> The receiver looks like this: When I try to send 5 bytes of payload : Note: edit: I have also tried to: < Ex> But this is wrong, because I am corrupting the stream while running it. Again this code: I do not know why you were If you get the result Passing a Is this code supposed to unpack the data you typed structure packet {int seqNum; Int ackNum; Int payload size; Four * payload; }; -------------------------------------- // MTU = 1460 characters * payload = (four * Malloc (MTU); Int BitesCopyrid = Sprint (payload, "% d% d% d", dataty-> seconds.Secum, datat-> seconds.cnm, datat-> gt; secondsloadload); Cout & lt; & Lt; "Bytes copied:" & lt; & Lt; Bytespeed & lt; & Lt; Endl; // print 3 - will it print 12 (3 * sizeof (int))? Dome (payload, dataty-> second plywood); BytesSent = sendto (mySocket, Payload, Packet Size, 0, (Const Structured Socoders *) and Toonodes, Length);
int n; Four buffer [MTU]; Berzo (buffer, MTU); Unsigned int length = sizeof (struct sockaddr_in); From the structure sockaddr_in; N = recvfrom (mySocket, buffer, MTU, 0, (Structure of Socadar *) and from, and Length); If (n & lt; 0) malfunction ("recvfrom"); Else {char * seqNum = (four *) malloc (sizeof (int)); Memcpy (seqNum, and buffer, sizeof (int)); Cout & lt; & Lt; "Segmentum:" & lt; & Lt; Seqnum & lt; & Lt; Endl; }
105 ##### - Receiver output:
Sankanum: 105 # . When I send:
1010 ########## (10 bytes of payload) receiver puts:
SeqNum: 1010 .
mempi (payload, and dataty-> sec.Secondum, size); Payload + = size (difference); Memcpy (payload, & amp; data is-> second.ackNum, sizeof (int)); Payload + = size (difference); Mempie (payload, & datite-> second payload size, size (intestine)); Payload + = size (difference); Dome (payload, dataty-> second plywood); Payload - = packet size; //sizeof (dataIt->.second.payload) + sizeof (int) + sizeof (int) + sizeof (int) bytescent = sendto (mySocket, payload, packet size, 0, (const struct sockaddr *) and toNode, length );
int bytescipid = sprint (payload, "% D% d% d", dataty-> seconds.Secum, dataty-> second.cnm, dataty-> second .payloadSize); Cout & lt; & Lt; "Bytes copied:" & lt; & Lt; Bytespeed & lt; & Lt; Endl;
bytes copied 12. There is nothing to do with
sizeof (int) ; The base can be converted into 10 digits using
Sprint ("% d" .) The length can be anywhere from
3 to
30 Or even if your
int s is greater than 32-bit.
3 , then it indicates that all those integers This is not a very good data format, because the other end does not know how to unpack it. The receiver looks at
"12345" . Whether you sent
12
34
5 , or
1
23
45 ?
bytesSent = sendto (mySocket, payload, packet size, 0, (const struct sockaddr *) & amp; toNode, length);
length and
packet sizes are not defined anywhere.
Four * SecNum = (Four *) maulok (size); mempi (sikanum, and buffer, sizaf (int)); COAT & lt; & lt; "section:" & lieutenant; & Lt; To Qnum & lt; & Lt; Endl;
char * to
cout in this way means that it expects a redundant string. However, (if there is actually a pack int in
buffer ), then you are sending it four bytes that do not make the string.
sprintf ? Because if this is the case, then it is not that you are very mixed on the difference between
int , and represent the base 10 of that int.
Comments
Post a Comment