Weird behavior of send() and recv()
<p>SORRY FOR BAD ENGLISH</p>

<p>Why if I have two <strong><code>send()</code></strong>-s on the server, and two <strong><code>recv()</code></strong>-s on the client, sometimes the first <code>recv()</code> will get the content of the 2nd <code>send()</code> from the server, without taking just the content of the first one and let the other <code>recv()</code> to take the "due and proper" content of the other <code>send()</code>?</p>

<p>How can I get this work in an other way?</p>

<p>This is by design.</p>

<p>A TCP stream is a channel on which you can send bytes between two endpoints but the transmission is stream-based, not message based.</p>

<p>If you want to send messages then you need to encode them... for example by prepending a "size" field that will inform the receiver how many bytes to expect for the body.</p>

<p>If you send 100 bytes and then other 100 bytes it's well possible that the receiver will instead see 200 at once, or even 50 + 150 in two different read commands. If you want message boundaries then you have to put them in the data yourself.</p>

<p>There is a lower layer (datagrams) that allows to send messages, however they are limited in size and delivery is not guaranteed (i.e. it's possible that a message will get lost, that will be duplicated or that two messages you send will arrive in different order).
TCP stream is built on top of this datagram service and implements all the logic needed to transfer data reliably between the two endpoints.</p>

<p>As an alternative there are libraries designed to provide reliable message-passing between endpoints, like <a href="http://zeromq.org/" rel="nofollow">ZeroMQ</a>.</p>

python