home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / question / 15452 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.5 KB  |  59 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!mcsun!inesc.inesc.pt!dec4pt.puug.pt!bvl!vasco
  3. From: vasco@bvl.pt (antonio vasconcelos)
  4. Subject: Re: Sockets: How do I detect when it has unexpectedly closed
  5. Message-ID: <1993Jan12.114400.3620@bvl.pt>
  6. Organization: Bolsa de Valores de Lisboa
  7. X-Newsreader: Tin 1.1 PL4
  8. References: <1993Jan11.200716.14442@bvl.pt>
  9. Date: Tue, 12 Jan 1993 11:44:00 GMT
  10. Lines: 47
  11.  
  12. ejohnson@void.ncsa.uiuc.edu (Eric E. Johnson) writes:
  13. : I know how to close a connection.  That's easy.  My question is: how do
  14. : I detect that a socket (AF_INET, SOCK_STREAM) has been unexpectedly 
  15. : disconnected from its remote client?  I'm assuming a case where the
  16. : remote client has crashed and was unable to send out a "closing"
  17. : message.
  18.  
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <sys/un.h>
  22. #include <stdio.h>
  23. #include <netinet/in.h>
  24. #include <netdb.h>
  25. #include <signal.h>
  26.  
  27. int s;    /* this is the socket, it's external to main in order */
  28.     /* to be closed by byebye() and snifserver() */
  29.  
  30. byebye()
  31. {
  32.     close(s);
  33.     fprintf(stderr,"aghhhhhhh... you got me...");
  34.     abort();
  35. }
  36.  
  37. snifserver()
  38. {
  39.     fprintf(stderr,"The server is dead... R.I.P.\n\n");
  40.     close(s);
  41.     exit(1);
  42. }
  43.  
  44. then, in the main() you just have to setup signal handlers.
  45.  
  46.     signal(SIGINT,byebye);
  47.     signal(SIGPIPE,snifserver);
  48.  
  49. By the way, if the client dies, the server should recive a SIGPIPE too.
  50.  
  51.  
  52. I use this method in a NeXT running NS 2.2.
  53.  
  54. -- 
  55. regards,
  56.   ____
  57. |/asco
  58. ----------------------------------------------------------------------
  59.