Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is usually the result of something like:</p> <pre><code>/* BAD CODE */ const char* ack = "ACK"; err = write( sockfd, ack, strlen( ack )); /* sender */ /* ... */ char buf[SOME_SIZE] readb = read( sockfd, buf, SOME_SIZE ); /* receiver */ printf( "%s", buf ); </code></pre> <p>The problem with the code above is that sender writes only three (3) bytes to the socket. That does not include string zero-terminator. Then the receiver gets the data and either does not check system call return value at all or/and blindly prints the received data. The <code>printf</code> will print everything until it finds zero-valued byte in memory.</p> <h3>Edit:</h3> <p>Based on your comments I think you are assuming that one <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/send.2.html" rel="nofollow"><code>send(2)</code></a> over TCP socket should result in one <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/recv.2.html" rel="nofollow"><code>recv(2)</code></a> on the other end with corresponding number of bytes (I'm guessing this is what you mean by "number of bytes read being wrong"). This is not the case with TCP though. You have to treat the socket as a stream that can give you arbitrary-sized chunks of what has been sent to you. It's your job to put them back together and recognize application message boundaries. This simply means you always read from a socket in a loop (not counting non-blocking designs with <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/select.2.html" rel="nofollow"><code>select(2)</code></a> and friends - that's a separate topic).</p> <p>Two accepted application-level protocol designs are:</p> <ul> <li>Communicate via pre-defined fixed-length messages - this way you read until you get that many bytes from the socket. Simple.</li> <li>Include message type and/or message length into the message itself - this is usually done with fixed-length message header that is followed by varying message payload. Read until you get full header then switch/dispatch/continue reading depending on type/length.</li> </ul> <p>Don't forget about <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow">endianess</a> - networks like <a href="http://en.wikipedia.org/wiki/Network_byte_order#Endianness_in_networking" rel="nofollow">network byte order</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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