home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!void!ejohnson
- From: ejohnson@void.ncsa.uiuc.edu (Eric E. Johnson)
- Subject: Re: Sockets: How do I detect when it has unexpectedly closed (solved)
- References: <1993Jan13.040639.18416@slate.mines.colorado.edu> <1993Jan15.174830.2021@bvl.pt> <ejohnson.727557988@void> <ejohnson.727639716@void>
- Message-ID: <ejohnson.727891495@void>
- Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
- Organization: University of Illinois at Urbana
- Date: Sun, 24 Jan 1993 16:04:55 GMT
- Lines: 32
-
- I solved my problem of detecting when a socket closes. When I first
- asked, there were two methods proposed. One was to catch SIGPIPE. When
- one writes or reads from a broken socket, this signal is generated. The
- second method was to watch the number of bytes returned from a blocking
- read. If a blocking read returned zero, then the socket is bad.
-
- I tried to catch SIGPIPE, but the damn signal was never generated. I
- tried lots of possibilities, like putting the signal(SIGPIPE,myfunc),
- right before the read, in main(), and what not. Nothing happened.
-
- I even tried to look through the netstat source code, but alas, that was
- reading the kernel's memory, and I don't exactly have root access.
-
- Since I was using nonblocking sockets, the blocking read that returned
- zero was right out, because when a nonblocking read returns zero, that's
- nothing unusual. However, you can do it like this...
-
- When a socket becomes disconnected, a select(fd_set,&readSet,...) will
- return readSet with one of the bits set, indicating that the socket had
- data to read. If, you use a nonblocking socket, read will return a
- zero. The only time one can interpret this zero to mean a disconnected
- socket is when select() is used.
-
- The great part is that you know which socket has been disconnected so
- that one can shut the connection and release whatever memory was
- allocated for it.
-
- Eric
-
- --
- Eric E Johnson | "Sigsoft is a black hole of ambition, everything
- ejohnson@a.cs.uiuc.edu | gets sucked in, and nothing comes out." - Me
-