I am writing some kind of server application (in C++), which holds an fd_set of file descriptors of sockets the server is communicating with, and I wish to know when one of them is closed\shut down on the other side without having to read\write to it. I routinely block on select() (with no time out), waiting for read readiness and exceptional conditions of the two fd_set I supply it with.
I started out thinking that any socket being closed on the other side would appear in the exceptfds, but as I read on I came to understand that it is not necessarily the case. The select() man page does not really say much about what exceptions exceptfds is being watched for. Further more, to my understanding what is considered as an exceptional condition changes from OS to OS (I myself am using Ubnubt 11.10), and I have not found anywhere a reference to a socket being closed\shut down (on the other side) as an exception.
Also, the socket(7) man page mentions two kinds of signals that a socket may send:
- SIGPIPE – Is sent when writing into a shut down socket.
- SIGIO – Is sent when an I/O event occurs.
Unfortunately, it is not mentioned what constitutes as an I/O event, and anyway shutting down might not be considered as an I/O event by everyone (I myself am a good example).
So what I’m asking is – Can I make a socket either send a signal or insert itself into exceptfds when it is closed, or to actively prompt me in some other way?
Thanks,
Shay
Usually, when a socket is closed by the remote peer, it becomes readable (for the purposes of
select()), and when you read from it you get zero bytes back. This zero byte read indicates that the other end closed the socket normally. This is not considered an exceptional condition.