Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>On the server side, you just have to reverse what you've done on the client side:</p> <p>You're also not gaining anything by using <code>char *pointer = buf</code>, just read directly into the buffer. When you loop around for your <code>MAXPROFILES</code>, just check your current position in <code>buffer</code> against the returned <code>size</code> to make sure you're not reading data you didn't receive.</p> <p>For example:</p> <pre><code> int size = read(newsockfd, buffer, sizeof(buffer)); int src, dst; char ver; short n; memcpy(&amp;src, buf + 0, 4); memcpy(&amp;dst, buf + 4, 4); memcpy(&amp;ver, buf + 8, 1); memcpy(&amp;n, buf + 9, 2); </code></pre> <p>...and so on...</p> <p>You're encoding your structures/data on the client side, so you have to decode them on the server side. And once you've done so, then you can print them or do whatever it is you like:</p> <pre><code> printf("src: %d dst: %d ver: %c n: %d\n", src, dst, ver, n); </code></pre> <p>Also, to simplify your encode/decode, use the same method (e.g. <code>memcpy</code> instead of your byte-by-byte with masks method). For example, on the client side:</p> <pre><code> k = 0; memcpy(buf + k, &amp;src, sizeof(src)); k += sizeof(src); memcpy(buf + k, &amp;dst, sizeof(dst)); k += sizeof(dst); ... </code></pre> <p>This also frees you a bit from hardcoded numbers as I was doing above. Just make sure your <code>src</code>, <code>dst</code>, and all the rest have the same type on both the client and server. </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