home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!mcsun!fuug!kiae!ntc!news-server
- From: saty@ntc.togliatti.su (Sergey A. TsYbanov)
- Subject: Re:nonblocking socket
- Message-ID: <ANivugg0S2@ntc.togliatti.su>
- Lines: 109
- Sender: news-server@ntc
- Reply-To: saty@ntc.togliatti.su
- Organization: AutoVAZ Research & Development Centre
- Date: Mon, 7 Sep 1992 17:16:28 GMT
-
- Hi !
- tasos@cs.bu.edu (Anastasios Kotsikonas) writes to ALL:
- > Hi all,
- > assuming that a reader process reads from a nonblocking socket, how
- > can it know that the other end has close()'d?
-
- In this programm function AsyncCheck does it asynchronously.
- My operation system is Ultrix-32, version 3.1 .
-
- --- CUT HERE ---
-
- #include<sys/types.h>
- #include<sys/time.h>
- #include<sys/socket.h>
- #include<sys/ioctl.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #include<stdio.h>
- #include<errno.h>
- #define PORT_NUMBER 13000
-
- typedef enum { NOTREADY, ISREADY, CLOSED } status_t;
-
- static status_t AsyncCheck( sock)
- int sock;
- {
- struct timeval tt;
- fd_set msk;
- tt.tv_sec= 0;
- tt.tv_usec= 0;
- FD_ZERO(&msk);
- FD_SET(sock, &msk);
- switch ( select( sock+1, &msk, (int *)0, (int *)0, &tt ) ) {
- case 0:
- return NOTREADY;
- case -1:
- perror("select");
- exit(1);
- default:
- if( 0>ioctl( sock, FIONREAD, &sock ) )
- perror("ioctl FIONREAD"), exit(1);
- return sock>0? ISREADY : CLOSED;
- }
- }
-
- static void client(){
- int s;
- struct sockaddr_in addr_in;
- char hostnm[ 32];
- struct hostent *hent;
- puts("Waiting for server to get ready");
- sleep(2);
- if( gethostname( hostnm, 32 ) < 0 ) perror("gethostname"), exit(1);
- hent=gethostbyname( hostnm );
- if( hent == ( struct hostent *) 0 ) perror(hostnm), exit(1);
- s= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
- if( s < 0) perror("socket"), exit(1);
- addr_in.sin_family= AF_INET;
- addr_in.sin_port= htons(PORT_NUMBER);
- bcopy( hent->h_addr, (char *) &addr_in.sin_addr, hent->h_length );
- if( connect( s, &addr_in, sizeof addr_in) < 0 )
- perror("connect"), exit(1);
- if( ioctl( s, FIONBIO, &s ) < 0 ) perror("ioctl FIONBIO"), exit(1);
- while( AsyncCheck( s) != CLOSED )
- puts("Server exists");
- puts("Client exiting...");
- }
-
- static void server() {
- int s0, s1;
- struct sockaddr_in addr_in;
- s0= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
- if( s0 < 0) perror("socket"), exit(1);
- bzero( (char *) &addr_in, sizeof addr_in);
- addr_in.sin_family= AF_INET;
- addr_in.sin_port= htons(PORT_NUMBER);
- if( bind( s0, &addr_in, sizeof addr_in) < 0 ) perror("bind"), exit(1);
- if( listen( s0, SOMAXCONN) < 0 ) perror("listen"), exit(1);
- puts("Server is ready");
- do
- s1= accept( s0, ( struct sockaddr_in *)0, ( int *)0 );
- while( s1< 0 );
- ( void) close( s0);
- sleep(6);
- puts("Server exiting...");
- }
-
- void main() {
- switch ( fork() ) {
- case 0: server();
- exit(0);
- case -1: perror("fork");
- exit(1);
- default : client();
- exit(0);
- };
- }
-
- --- CUT HERE ---
-
- best regards
- Saty from Togliatti.
- ! iH
- --
- ******************* Is There Anybody Out There ? **********************
- * SATY ( Sergey A. TsYbanov ). *
- * E-mail : saty@ntc.togliatti.su Phone : number +7 (8482) 378-17-53. *
- ***********************************************************************
-
-