*BSD News Article 60645


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!news.bhp.com.au!mel.dit.csiro.au!munnari.OZ.AU!news.hawaii.edu!ames!usenet.kornet.nm.kr!ns.etri.re.kr!news.kreonet.re.kr!usenet.seri.re.kr!news.imnet.ad.jp!news.join.ad.jp!newsxfer.itd.umich.edu!newsxfer2.itd.umich.edu!chi-news.cic.net!uwm.edu!math.ohio-state.edu!howland.reston.ans.net!usc!ccnet.com!usenet
From: Bill Richter <richterb@ccnet.com>
Newsgroups: comp.unix.bsd.freebsd.misc,comp.os.linux.development.apps
Subject: Re: Checking for a closed socket connection
Date: Sat, 27 Jan 1996 12:20:20 -0800
Organization: CCnet Communications (510-988-7140 guest)
Lines: 25
Message-ID: <310A8904.446B9B3D@ccnet.com>
References: <NEWTNews.822505959.29723.jalvarez@sundev.uno.com> <4e7l8n$50f@charm.il.ft.hse.nl> <4ea6cd$mq8@nestor.sv.vtcom.fr>
NNTP-Posting-Host: h98-192.ccnet.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: Mozilla 2.0b6a (X11; I; FreeBSD 2.1-STABLE i386)
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:13245 comp.os.linux.development.apps:11613

Bear in mind, depending on how you set *timeout in 
select, a return from select of 0 merely indicates
the timer expired with nothing read from the socket 
or in polling mode, nothing ready.  Yet the socket
can be connected to a peer. 

You could if size <= 0 then do a read() or getpeername()
Which would set errno and return a -1.
Catching SIGPIPE is good too, but takes a bit longer
to get the signal, depending on the state of your 
process.

> A more secure code would be something like that :
> 
>    ...
>    status = select(...)
>    if (status>0)
>    {
>       size = read(...);
>       if (size<=0) return(SOCKET_DOWN);
>    }
>    ...
> (note the '<=' and not just '<')
> 
> Pierre-Yves