title: recv() returns 0
body: <p>I have a very annoying problem that I found several times on other forums but I can't find a proper solution.
  The problem is recv() returns 0 on the last few bytes of a connection. Here are some background information.</p>
  
  <ul>
  <li>Both (client / server) applications run on the same machine.</li>
  <li>Both (client / server) sockets are non-blocking</li>
  <li>The transfered data size is 53 bytes.</li>
  <li>Both (client / server) call shutdown and closesocket when the last send()/recv() was executed.</li>
  <li>I also tried with SO_LINGER and 10 seconds, no success either</li>
  </ul>
  
  <p>I call send() several times (small chunks) and from the client side 53 bytes are transfered.
  The server calls recv() several times (4 byte requests) and read 49 bytes and then returns 0 (54 Bytes - 49 Bytes, so 4 bytes are missing).</p>
  
  <p>MSDN and some forums write for non-blocking sockets:</p>
  
  <ul>
  <li>recv() definitely returns &lt; 0 on error and errno / WSAGetLastError is set</li>
  <li>recv() definitely returns = 0 when the other side closed the connection</li>
  <li>recv() definitely returns > 0 when data was read</li>
  </ul>
  
  <p>MSDN also says:</p>
  
  <blockquote>
    <p>Using the closesocket or shutdown functions with SD_SEND or SD_BOTH
    results in a RELEASE signal being sent out on the control channel. Due
    to ATM's use of separate signal and data channels, it is possible that
    a RELEASE signal could reach the remote end before the last of the
    data reaches its destination, resulting in a loss of that data. One
    possible solutions is programming a sufficient delay between the last
    data sent and the closesocket or shutdown function calls for an ATM
    socket.</p>
  </blockquote>
  
  <p>This is regarded in the example of recv() and send(): <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx</a></p>
  
  <p>But still no success, I still get some interrupts in 10% of all connections after the 49 Byte is received, 90% of the connections succeed. Any ideas? Thx.</p>