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> 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);

The receiver looks like this:

  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; }   

When I try to send 5 bytes of payload : 105 ##### - Receiver output: Sankanum: 105 # . When I send: 1010 ########## (10 bytes of payload) receiver puts: SeqNum: 1010 .

Note:

  • Running both boxes (Ubuntu VM)
  • About portability at all There is no need to worry, now

    edit:

    I have also tried to: < Ex> 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 );

    But this is wrong, because I am corrupting the stream while running it.

    Again this code:

      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;   

    I do not know why you were 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.

    If you get the result 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;   

    Passing a 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.

    Is this code supposed to unpack the data you typed 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

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -