title: Handle multiple TCP clients
body: <p>I am writing a simple TCP server, and despite a lot of Googling I can't quite figure out the answer to a question of mine.</p>
  <p>I want my server to handle multiple clients.</p>
  <p>For performance and simplicity reasons, I want my server to be single-threaded, and I use <code>select()</code> to handle all the sockets.</p>
  <p>Also, my protocol is message-based. Message framing is done using a length prefix.
  This means the server needs a buffer to reconstruct messages fragment after fragment.</p>
  <p>The maximum message length is 64K bytes (although I could reduce it to 256 bytes).</p>
  <p>Note: this will run on a tiny embedded device, so using a messaging layer like ZMQ is not an option (not enough memory).</p>
  <p>I can either:</p>
  <ol>
  <li><p>have a single buffer, but it means once I started to receive/reconstruct a message from a client socket, I ignore other client sockets until the current message has been completely received. This is prone to DOS attacks: one client sending a huge message, byte per byte, very slowly, would block the server.</p>
  </li>
  <li><p>have one buffer per client socket, and then my server would be really parallel. But it does not scale well with the number of clients.</p>
  </li>
  </ol>
  <p>Is there another way which has the benefits of both techniques and none of the drawbacks?</p>
  <p>One idea I had was to use the socket buffer to store the entire message.
  I would set the buffer size to 64K using <code>setsockopt()</code> and <code>SO_RCVBUFSIZ</code>.</p>
  <p>But I would need to do a <code>recv()</code> with both <code>MSG_WAITALL</code> and <code>MSG_DONTWAIT</code>, so that either the message is entirely available in the socket buffer and I get it, or it is not entirely received yet and then <code>recv()</code> does not block. However, these 2 options do not work together.</p>
  <p>Maybe I can do a <code>recv()</code> with <code>MSG_PEEK</code> to read the size, then another <code>recv()</code> with <code>MSG_PEEK</code> to test if all bytes are available, and if yes then re-do a <code>recv()</code> without <code>MSG_PEEK</code> to actually read the bytes from the socket?</p>
  <p>Anyway, I have the impression it is a trivial question that must have been solved a long time ago.</p>