I have a very unpleasant problem that I found several times in other forums, but I can not find the right solution. The problem is that recv () returns 0 in the last few bytes of the connection. Here is some background information.
- Both applications (client / server) run on the same computer.
- Both (client / server) sockets are not blocked
- The size of the transmitted data is 53 bytes.
- Both (client / server) end the call and close when the last send () / recv () is completed.
- I also tried SO_LINGER and 10 seconds failed.
I call send () several times (small chunks), and 53 bytes are sent from the client side. The server calls recv () several times (4 byte requests) and reads 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 the output of the RELEASE signal to the control channel. In connection with the use of ATMs by separate signal and data channels, it is possible that the RELEASE signal can reach the far end until the last data reaches its destination, which leads to the loss of this data. One possible solution is to program a sufficient delay between the last sent data and the close or shutdown function for the ATM socket.
recv() send(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx
, 10% 49 , 90% . ? .