*BSD News Article 60503


Return to BSD News archive

#! rnews 2071 bsd
Newsgroups: comp.unix.bsd.freebsd.misc,comp.os.linux.development.apps,comp.unix.programmer
Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.rmit.EDU.AU!news.unimelb.EDU.AU!munnari.OZ.AU!spool.mu.edu!howland.reston.ans.net!newsfeed.internetmci.com!in2.uu.net!istar.net!infoshare!whome!light-house!innuendo.tlug.org!brutus!vic
From: vic@brutus.tlug.org (Vic Metcalfe)
Subject: Re: Checking for a closed socket connection
Followup-To: comp.unix.programmer
X-Newsreader: TIN [version 1.2 PL2]
Reply-To: vic@brutus.tlug.org
Organization: Zymurgy Systems, Aurora, Ontario, Canada
Message-ID: <1996Jan25.135629.7187@brutus.tlug.org>
References: <NEWTNews.822505959.29723.jalvarez@sundev.uno.com>
Date: Thu, 25 Jan 1996 13:56:29 GMT
Lines: 28
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:13133 comp.os.linux.development.apps:11495 comp.unix.programmer:33559

jalvarez@uno.com wrote:

: Is there a simple way to test a socket descriptor for a closed
: connection (e.g. if the connection to the other side was lost
: and is no longer valid) ?

Though I've heard that read() and write() should return -1 in this condition,
it is my experience with linux that read() and write() return 0.  I have had
some success with checking for a return of <= 0.  If I persist in my reads
and writes, I eventually get an EPIPE error with a -1 return.

According to previous posts I have read, this is valid.  Some other systems
will return ECONNRESET, or EINVAL in errno.

Is checking for the 0 return from read()/write() a bad idea?  Can this happen
under normal conditions?

BTW, questions of this sort are usually dealt with in comp.unix.programmer,
since the answer should be generic across unix platforms.  I have added
comp.unix.programmer to the cross-post list, and directed all followups to
comp.unix.programmer alone.  I am working on a unix-socket-faq for that
newsgroup which I hope to post on the weekend.  

The above answer by the way is taken word for word from the faq.  Please
post a follow-up if you see an error in it.

Thanks,
  Vic.