recv() after disconnect
I'm trying to send data through the internet.
Is the recv() function supposed to receive all data after the send() function has sent all and disconnected after?
Or are the two sides supposed to stay connected until the recv() side received all bytes?
I believe the short answer is not necessarily so.
The solution is to use setsockopt() with the SO_LINGER option which means close()
blocks until the closing handshake completes.
If you google on SO_LINGER you'll probably get a good explanation of why.
What type of socket you are using?
For TCP Close causes to put the FIN packet in the outgoing stream, so the other side will have to read all the data before reaching this FIN packet. Of cause after closing the sender side socket you will not be able to receive transmission error notifications in case of asynchronous socket.
I'm using TCP socket.
Actually it will still deliver on a LAN after disconnect() is called even without SO_LINGER.
But it wont over the Internet.
I'm still to try SO_LINGER through the Internet.
Apparently SO_LINGER is broken, see bottom of the page.
Graceful Shutdown, Linger Options, and Socket Closure (Windows)
I think this article covers all the angles well.
The reset action of TCP on close() if there is still data (even a stray byte) in the RECVQ is something to be aware of.
btw my reading of the bug report is that the problem is with WSASendDisconnect() not SO_LINGER.