home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15897 < prev    next >
Encoding:
Text File  |  1993-01-24  |  2.1 KB  |  44 lines

  1. Newsgroups: comp.unix.questions
  2. 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
  3. From: ejohnson@void.ncsa.uiuc.edu (Eric E. Johnson)
  4. Subject: Re: Sockets: How do I detect when it has unexpectedly closed (solved)
  5. References: <1993Jan13.040639.18416@slate.mines.colorado.edu> <1993Jan15.174830.2021@bvl.pt> <ejohnson.727557988@void> <ejohnson.727639716@void>
  6. Message-ID: <ejohnson.727891495@void>
  7. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  8. Organization: University of Illinois at Urbana
  9. Date: Sun, 24 Jan 1993 16:04:55 GMT
  10. Lines: 32
  11.  
  12. I solved my problem of detecting when a socket closes.  When I first
  13. asked, there were two methods proposed.  One was to catch SIGPIPE.  When
  14. one writes or reads from a broken socket, this signal is generated.  The
  15. second method was to watch the number of bytes returned from a blocking
  16. read.  If a blocking read returned zero, then the socket is bad.
  17.  
  18. I tried to catch SIGPIPE, but the damn signal was never generated.  I
  19. tried lots of possibilities, like putting the signal(SIGPIPE,myfunc),
  20. right before the read, in main(), and what not.  Nothing happened.
  21.  
  22. I even tried to look through the netstat source code, but alas, that was
  23. reading the kernel's memory, and I don't exactly have root access.
  24.  
  25. Since I was using nonblocking sockets, the blocking read that returned
  26. zero was right out, because when a nonblocking read returns zero, that's
  27. nothing unusual.  However, you can do it like this...
  28.  
  29. When a socket becomes disconnected, a select(fd_set,&readSet,...) will
  30. return readSet with one of the bits set, indicating that the socket had
  31. data to read.  If, you use a nonblocking socket, read will return a
  32. zero.  The only time one can interpret this zero to mean a disconnected
  33. socket is when select() is used.
  34.  
  35. The great part is that you know which socket has been disconnected so
  36. that one can shut the connection and release whatever memory was
  37. allocated for it.
  38.  
  39. Eric
  40.  
  41. -- 
  42. Eric E Johnson             | "Sigsoft is a black hole of ambition, everything
  43. ejohnson@a.cs.uiuc.edu     |  gets sucked in, and nothing comes out." - Me
  44.