Messages in this thread | | | Date | Sat, 19 Feb 2000 22:32:58 -0500 (EST) | From | "Richard B. Johnson" <> | Subject | Re: Network socket disconnect unreliable |
| |
On Sat, 19 Feb 2000, Alan Cox wrote:
> > When a network socket disconnects, this fact can't presently be > > reliably detected when using select. The exception bit is set, but this > > It can be > > > So, how am I supposed to know if the socket was disconnected? > > There are of course hacks. I can just count the number of times > > that this thing looped without finding anything to do and then > > exit if it's obvious that it's hung. > > If a socket is closed by the other end then it goes EOF for read once all > the data has been read. This is a portable property of sockets, its true > of BSD and its defined by posix 1003.1g drafts. So you do > > > if(FD_ISSET(fd, &read_mask)) > { > int len=read(fd, buffer, bufsize); > if(len==0) > { > /* EOF - closed */ > } > if(len<0) > { > if(errno==EAGAIN) > { > /* woken by something else */ > /* Not needed in theory but is for porting */ > } > else > { > /* it broke */ > } > } > } > > Alan
Yep. Wonderful. When the socket disconnects, select doesn't return! It only returns if the exception mask is enabled. So maybe I should use an exception mask to get select() to return but ignore it. The read/write code does as you show, but without the exception mask select() doesn't return after a disconnect. At least not on the 'current' versions of Linux (up to 2.3.41).
Cheers, Dick Johnson
Penguin : Linux version 2.3.41 on an i686 machine (800.63 BogoMips).
- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/
| |