Re: Network socket disconnect unreliable

From: Alan Cox (alan@lxorguk.ukuu.org.uk)
Date: Fri Feb 18 2000 - 19:39:03 EST


> 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

                
        

        

-
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/



This archive was generated by hypermail 2b29 : Wed Feb 23 2000 - 21:00:22 EST