*BSD News Article 28127


Return to BSD News archive

Xref: sserve comp.unix.programmer:15506 comp.unix.bsd:13524
Newsgroups: comp.unix.programmer,comp.unix.bsd
Path: sserve!newshost.anu.edu.au!munnari.oz.au!ihnp4.ucsd.edu!swrinde!gatech!newsxfer.itd.umich.edu!nntp.cs.ubc.ca!utcsri!newsflash.concordia.ca!sifon!nash.pubnix.qc.ca!fortress!andrew
From: andrew@fortress.pubnix.qc.ca (Andrew)
Subject: Re: Detecting dead client in BSD socket
Message-ID: <CMApnn.GxJ@fortress.pubnix.qc.ca>
Followup-To: comp.unix.programmer,comp.unix.bsd
Organization: PubNIX Montreal
X-Newsreader: TIN [version 1.2 PL2]
References: <1994Mar3.154852.24090@il.us.swissbank.com>
Date: Mon, 7 Mar 1994 13:04:34 GMT
Lines: 30

Dwight Lee (leed@il.us.swissbank.com) wrote:
: Is there a way to determine, from the server side, whether a client has
: closed its end of the connection in a BSD socket?  If the server attempts
: a write to the dead client, SIGPIPE is received, and that works if
: communication is steady and regular, but I'm wondering if there's a way
: to determine client death/closure at an arbitrary moment.

: If this is a FAQ, feel free to refer me to it, or any in-print books for
: that matter.

: Thank you.

If you've opened your socket in O_NONBLOCK (O_NDELAY), and have posted a
select call on it (IMHO the cleanest way), you will be interrupted with a
read or write select on the socket (Not an exception..haven't figured that
one out yet), and you will either read or write 0 bytes from/to the socket.

Careful inspection of the man page for read/write reveals that reading 0 bytes
indicates a closed socket at the remote end.  You can then safely close 
yourself down.

From read man page:
               set to the value returned in the error message.  If a hangup
               occurs on the stream being read, read will continue to
               operate normally until the stream head read queue is empty.
               Thereafter, it will return 0.

-- 
   Andrew Webster
Captain@PubNIX.qc.ca