title: Can a C socket recv 0 bytes without the client shutting the connection?
body: <p>I've implemented a web server in C.  It calls <code>recv()</code> on a connected, blocking socket to receive an incoming HTTP request.  The Linux man pages state the following about <code>recv()</code> on a blocking socket:</p>
  <blockquote>
  <p>If  no messages are available at the socket, the receive calls wait for a message to arrive...The receive calls normally return any data  available,  up  to  the requested amount, rather than waiting for receipt of the full amount requested.</p>
  <p>...</p>
  <p>These  calls  return  the  number  of bytes received, or -1 if an error occurred...The  return  value  will  be  0 when the peer has performed an orderly shutdown.</p>
  </blockquote>
  <p>Hence, to receive the full request, my server code contains a loop of the following form:</p>
  <pre><code>int connfd; // accepted connection
  int len;    // # of received bytes on each recv call
  
  ...
  
  while (/* HTTP message received so far has not terminated */) {
    len = recv(connfd, ...);
    if (len == 0) {
      // close connfd, then escape loop
    } else if (len &lt; 0) {
      // handle error
    } else {
      // process received bytes
    }
  }    
    
  </code></pre>
  <p>My question: is it possible for <code>recv()</code> to return 0 bytes due to network issues and without the client performing an orderly shutdown, thereby causing my code to exit the loop prematurely?  The man pages are ambiguous.</p>