I have a c++ program using winsock2. I would like to know how to tell when someone's connection to my program closes.
+5
A:
Use select to wait for reading on the socket; when the socket is closed winsock should report it as readable. Receiving from the socket will then give you 0 bytes, telling you that the socket was closed.
Martin v. Löwis
2009-10-05 21:48:40
When you check the return value of `recv` it can return *either* 0, meaning that the socket was closed successfully (as you describe), or `SOCKET_ERROR` which tells you the socket wasn't closed cleanly (e.g. timeout) or an error has occured on your side. Both conditions must be checked in order to catch all cases where the connection is closed.
GRB
2009-10-05 21:59:52