*BSD News Article 60147


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.ecn.uoknor.edu!news.uoknor.edu!news.nodak.edu!netnews1.nwnet.net!news.u.washington.edu!uw-beaver!uhog.mit.edu!news.kei.com!nntp.coast.net!swidir.switch.ch!in2p3.fr!univ-lyon1.fr!pasteur.fr!jussieu.fr!rain.fr!vtcom.fr!news
From: pyk@sv.vtcom.fr (Pierre-Yves Kerembellec)
Newsgroups: comp.unix.bsd.freebsd.misc,comp.os.linux.development.apps
Subject: Re: Checking for a closed socket connection
Date: Fri, 26 Jan 1996 09:02:48 GMT
Organization: Solutions Vocales
Lines: 27
Message-ID: <4ea6cd$mq8@nestor.sv.vtcom.fr>
References: <NEWTNews.822505959.29723.jalvarez@sundev.uno.com> <4e7l8n$50f@charm.il.ft.hse.nl>
Reply-To: pyk@sv.vtcom.fr
NNTP-Posting-Host: pyk2.sv.vtcom.fr
X-Newsreader: Forte Free Agent 1.0.82
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:12871 comp.os.linux.development.apps:11252

robert@il.ft.hse.nl (Robert Klep) wrote:

>jalvarez@uno.com:
> >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) ?
>read() to/write() from it and see if the function returns a -1 :)

You can also use a "setsockopt" system-call and set the
 SO_KEEPALIVE flag for that particular socket : your process 
will receive a SIGPIPE if the peer hangs up
(of course, it's your duty to handle that signal properly)
By the way, the "read" system-call won't return -1 in any case (trust me !)
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