Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue receiving and storing data in C++
    text
    copied!<p>I have written a server application in C++ that receives a packet of data from a client application (which I have not written myself) and prints the data to the console. The issue is, when I try to receive and store the entire packet body at once, the data is stored incorrectly, however when the packet body is received and stored using multiple callings of recv(), it does store correctly.</p> <p>Regarding endianness, the client and server are both running on a little endian machine, the client sends data out as little endian, and the server reads it without need for conversion.</p> <p><strong>This is the packet that the client application sends to the server application:</strong></p> <pre><code>00 03 23 00 57 6f 57 00 01 0c 01 f3 16 36 38 78 00 6e 69 57 00 42 47 6e 65 00 00 00 00 7f 00 00 01 05 41 44 4d 49 4e </code></pre> <p><strong>Here is a structured view of the packet:</strong></p> <pre><code>cmd 00 error 03 pkt_size 23 00 gamename 57 6f 57 00 version1 01 version2 0c version3 01 build f3 16 platform 36 38 78 00 os 6e 69 57 00 country 42 47 6e 65 timezone_bias 00 00 00 00 ip 7f 00 00 01 srp_I_len 05 srp_I 41 44 4d 49 4e </code></pre> <p><strong>These are the expected results to be printed out by the server application:</strong></p> <pre><code>cmd: 0 error: 3 pkt_size: 35 gamename: 5730135 version1: 1 version2: 12 version3: 1 build: 5875 platform: 7878710 os: 5728622 country: 1701726018 timezone_bias: 0 ip: 127 0 0 1 srp_I_len: 5 srp_I: ADMIN </code></pre> <p><strong>Here is the code which I am having trouble with:</strong></p> <pre><code>struct packet{ uint8 cmd; uint8 error; uint16 pkt_size; uint32 gamename; uint8 version1; uint8 version2; uint8 version3; uint16 build; uint32 platform; uint32 os; uint32 country; uint32 timezone_bias; uint8 ip[4]; uint8 srp_I_len; uint8 srp_I[16]; }; packet data; recv(clientSocket, &amp;data.cmd, 4, 0); // Receive packet header recv(clientSocket, &amp;data.gamename, 46, 0); // Receive packet body printf("%d\n", data.cmd); ... printf("%s\n", data.srp_i); </code></pre> <p><strong>The result:</strong></p> <pre><code>cmd: 0 error: 3 pkt_size: 35 gamename: 5730135 version1: 1 version2: 12 version3: 1 build: 13846 (this is where it all goes wrong) platform: 1466527232 os: 1850163712 country: 101 timezone_bias: 35512 ip: 1 5 65 68 srp_I_len: 77 srp_I: IN </code></pre> <p><strong>If I change the code like so:</strong></p> <pre><code>recv(clientSocket, &amp;data.cmd, 4, 0); // Receive packet header recv(clientSocket, &amp;data.gamename, 7, 0); // Receive packet body recv(clientSocket, &amp;data.build, 39, 0); // Receive packet body </code></pre> <p><strong>The result:</strong></p> <pre><code>... same expected results build: 5875 (fixed) platform: 1768816760 (goes all wrong here instead) os: 1195507799 country: 25966 timezone_bias: 8323072 ip: 0 1 5 65 srp_I_len: 68 srp_I: MIN </code></pre> <p><strong>And if I make one last adjustment to the code, like so:</strong></p> <pre><code>recv(clientSocket, &amp;data.cmd, 4, 0); // Receive packet header recv(clientSocket, &amp;data.gamename, 7, 0); // Receive packet body recv(clientSocket, &amp;data.build, 2, 0); // Receive packet body recv(clientSocket, &amp;data.platform, 37, 0); // Receive packet body </code></pre> <p><strong>The result:</strong></p> <pre><code>... same expected results build: 5875 platform: 7878710 os: 5728622 country: 1701726018 timezone_bias: 0 ip: 127 0 0 1 srp_I_len: 5 srp_I: ADMIN </code></pre> <p>By calling recv() multiple times, it received and stores the data exactly as expected. I have absolutely no idea why the data is stored incorrectly when only calling recv() twice. Please, somebody, enlighten me. Thank you.</p> <p>PS: Sorry for the monster post of ugliness.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload