title: Can we make a non-blocking server with blocking sockets?
body: <p>I have to make a simple IRC client/server programs for my IT school. The subject asks us to use <code>select(2)</code> for socket polling but forbids us to use <code>O_NONBLOCK</code> sockets.</p>
  
  <blockquote>
    <ul>
    <li>Your server will accept multiple simultaneous connections.<br>
    <strong>Attention, the use of <code>fork</code> is prohibited. So you should imperatively use <code>select</code></strong></li>
    <li>Your server must not be blocking.<br>
    <strong>This has nothing to do with non-blocking sockets, which are prohibited (so do not use <code>fcntl(s, O_NONBLOCK)</code>)</strong></li>
    </ul>
  </blockquote>
  
  <p>I’m wondering if it is even possible to design a non-blocking server (which does not fork) with blocking sockets even using <code>select(2)</code>.</p>
  
  <p>Here is a simple example: let say we have a simple text protocol with one command per line. Each client has a buffer. When <code>select(2)</code> tells us a client is ready for <code>read(2)</code>, we read until we found a <code>\n</code> in the client buffer, therefor we process the command. With non-blocking sockets, we would read until <code>EAGAIN</code>.</p>
  
  <p>Let imagine now that we are using blocking sockets and a malicious client sends text with no line-break. <code>select(2)</code> tells us data is available, we then <code>read(2)</code> on the client. But we will never read the expected <code>\n</code>. Instead of returning <code>EAGAIN</code>, the syscall will block indefinitely. This is a denial of service attack.</p>
  
  <p>Is it really possible to design a non-blocking server with blocking-sockets and <code>select(2)</code> (no <code>fork(2)</code>)?</p>