recv() returns 0

HelloWorld picture HelloWorld · May 10, 2012 · Viewed 31.9k times · Source

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.

  • Both (client / server) applications run on the same machine.
  • Both (client / server) sockets are non-blocking
  • The transfered data size is 53 bytes.
  • Both (client / server) call shutdown and closesocket when the last send()/recv() was executed.
  • I also tried with SO_LINGER and 10 seconds, no success either

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).

MSDN and some forums write for non-blocking sockets:

  • recv() definitely returns < 0 on error and errno / WSAGetLastError is set
  • recv() definitely returns = 0 when the other side closed the connection
  • recv() definitely returns > 0 when data was read

MSDN also says:

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.

This is regarded in the example of recv() and send(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx

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.

Answer

Remy Lebeau picture Remy Lebeau · May 10, 2012

recv() returns 0 only when you request a 0-byte buffer or the other peer has gracefully disconnected. If you are not receiving all of the data you are expecting, then you are not reading the data correctly to begin with. Please update your question with your actual code.