home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!mcsun!inesc.inesc.pt!dec4pt.puug.pt!bvl!vasco
- From: vasco@bvl.pt (antonio vasconcelos)
- Subject: Re: Sockets: How do I detect when it has unexpectedly closed
- Message-ID: <1993Jan12.114400.3620@bvl.pt>
- Organization: Bolsa de Valores de Lisboa
- X-Newsreader: Tin 1.1 PL4
- References: <1993Jan11.200716.14442@bvl.pt>
- Date: Tue, 12 Jan 1993 11:44:00 GMT
- Lines: 47
-
- ejohnson@void.ncsa.uiuc.edu (Eric E. Johnson) writes:
- : I know how to close a connection. That's easy. My question is: how do
- : I detect that a socket (AF_INET, SOCK_STREAM) has been unexpectedly
- : disconnected from its remote client? I'm assuming a case where the
- : remote client has crashed and was unable to send out a "closing"
- : message.
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <stdio.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <signal.h>
-
- int s; /* this is the socket, it's external to main in order */
- /* to be closed by byebye() and snifserver() */
-
- byebye()
- {
- close(s);
- fprintf(stderr,"aghhhhhhh... you got me...");
- abort();
- }
-
- snifserver()
- {
- fprintf(stderr,"The server is dead... R.I.P.\n\n");
- close(s);
- exit(1);
- }
-
- then, in the main() you just have to setup signal handlers.
-
- signal(SIGINT,byebye);
- signal(SIGPIPE,snifserver);
-
- By the way, if the client dies, the server should recive a SIGPIPE too.
-
-
- I use this method in a NeXT running NS 2.2.
-
- --
- regards,
- ____
- |/asco
- ----------------------------------------------------------------------
-