Can someone provide me an example of how to use select() to see if a client has closed the connection on a socket?
FYI. I'm using linux.
Thanks!
Can someone provide me an example of how to use select() to see if a client has closed the connection on a socket?
FYI. I'm using linux.
Thanks!
The below snippet first checks if the socket is marked readable (which it is when closed) and then if there's actually anything to be read.
If you get a read notification and the read returns 0 bytes the client has exited cleanly. If not you will get an exception notification.
If the client has closed cleanly you should get notification that READ will not block and EXCEPTION (third set) is flagged.
If the client has closed abortively you do not know. This is subject to the two generals problem.
You don't need to do a
select()
followed byioctl()
. You can instead do a non-blocking peek on the socket to see if it returns0
.