Subject: Re: Sensing a disconnected socket
To: Rick Byers <rickb@iaw.on.ca>
From: Andrew Brown <codewarrior@daemon.org>
List: current-users
Date: 06/19/1997 17:06:02
>Sorry this is a little off topic, but it might be partly NetBSD specific,
>so I'll ask anyway.

it's close enough...  :)

>I'd like to be able to tell when a socket (TCP Stream) has been
>disconnected without writing to it.  I've got a cgi that writes stuff now
>and then, but usually just waits.  If the user stops the transfer, I need
>the process to exit.  Normally, it just sits there with the socket in the
>FIN_WAIT_2 state until the process is killed.  I was writing a NUL to the
>socket every 2 seconds, which works, but isn't ideal.  If something gets
>writen when the socket is in that state, the process gets SIGPIPE and
>quits.  

...which you can catch and handle if you so choose...

>Ideally the process will exit as soon as the user stops the transfer, but
>as far as I can tell, no signal (SIGPIPE, SIGURG etc..) is delivered to
>the process when the socket in intially closed.  I can just have a 1
>second interval alarm to check (and possibly exit) if the socket has been
>closed.  How can I tell that it has been closed?  I can't seem to find any
>functions that will return that information.

if you're only ever writing to the socket and don't expect to ever
read from it, then selecting on the socket for reading will return
instantly after it's been closed.  or, selecting on it for writing
will return instantly (but will result in a sigpipe) after it's been
closed.  then again, this might be a better situation in which to use
the "exceptfds" which no one has ever been able to tell me what they
were for...

-- 
|-----< "CODE WARRIOR" >-----|
andrew@echonyc.com (TheMan)        * "ah!  i see you have the internet
codewarrior@daemon.org                               that goes *ping*!"
warfare@graffiti.com      * "information is power -- share the wealth."