Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are using TCP to communicate between your receiver and transmitter and TCP is a stream-oriented protocol. That is you put a stream of bytes in one end and you get the stream out on the other end, in order and with no loss. There is no guarantee that each send() will match a recv() on the other end as the data may be broken up for various reasons.</p> <p>So if you do the following with a TCP connection:</p> <pre><code>char buffer[] = "1234567890"; send(socket, buffer, 10, 0); </code></pre> <p>And then on the receiver:</p> <pre><code>char buffer[10]; int bytes = recv(socket, buffer, 10, 0); </code></pre> <p>bytes can be anywhere between 0 and 10 when recv() returns.</p> <p>TCP runs over IP which is a datagram oriented protocol. This is why the TCP implementation can assume that when it sends a datagram it will receive the entire datagram on the other end (or possibly not, or receive it out-of-order). If you want to simulate that you have at least two options:</p> <ol> <li>Add framing to your TCP messages so you can extract packets from it. This involves adding things like the size of the packet to a header that you send into the stream. It would be kind of meaningless to use this for simulating TCP as all your packets would always arrive, always in order and already using the underlying TCP flow control/congestion avoidance mechanisms.</li> <li>Use a datagram protocol such as UDP. This would be closer to the IP layer that TCP runs over.</li> </ol> <p>You should probably go with option 2 but if you want to go the framing route over TCP you can e.g. (rough quick code follows):</p> <pre><code>// We do this to communicate with machines having different byte ordering u_long packet_size = htonl(10); // 10 bytes packet send(socket, &amp;packet_size, 4, 0); // First send the frame size send(socket, buffer, 10, 0); // Then the frame </code></pre> <p>Receiving end:</p> <pre><code>u_long packet_size; // Hold the size of received packet int bytes_to_read = 4; // We send 4 bytes on the wire for size and expect 4 int nresult; // hold result of recv() char *psize = &amp;packet_size; // Point to first byte of size while( bytes_to_read ) // Keep reading until we have all the bytes for the size { nresult = recv(socket, psize, bytes_to_read, 0); if(nresult==0) deal with connection closed. bytes_to_read -= nresult; psize += nresult; } packet_size = ntohl(packet_size); // Now we know the packet size we can proceed and read it similar to above </code></pre>
    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.
 

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