home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / programm / 4555 < prev    next >
Encoding:
Text File  |  1992-09-08  |  3.2 KB  |  121 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!mcsun!fuug!kiae!ntc!news-server
  3. From:  saty@ntc.togliatti.su (Sergey A. TsYbanov)
  4. Subject: Re:nonblocking socket
  5. Message-ID: <ANivugg0S2@ntc.togliatti.su>
  6. Lines: 109
  7. Sender: news-server@ntc
  8. Reply-To: saty@ntc.togliatti.su
  9. Organization: AutoVAZ Research & Development Centre
  10. Date: Mon, 7 Sep 1992 17:16:28 GMT
  11.  
  12. Hi !
  13. tasos@cs.bu.edu (Anastasios Kotsikonas) writes to ALL:
  14. >       Hi all,
  15. >       assuming that a reader process reads from a nonblocking socket, how
  16. >       can it know that the other end has close()'d?
  17.     
  18. In this programm function AsyncCheck does it asynchronously.
  19. My operation system is Ultrix-32, version 3.1 .
  20.  
  21. --- CUT HERE ---
  22.  
  23. #include<sys/types.h>
  24. #include<sys/time.h>
  25. #include<sys/socket.h>
  26. #include<sys/ioctl.h>
  27. #include<netinet/in.h>
  28. #include<netdb.h>
  29. #include<stdio.h>
  30. #include<errno.h>
  31. #define PORT_NUMBER 13000
  32.  
  33. typedef enum { NOTREADY, ISREADY,  CLOSED }  status_t;
  34.  
  35. static status_t AsyncCheck( sock)
  36. int sock;
  37. {
  38.   struct timeval tt;
  39.   fd_set msk;
  40.   tt.tv_sec= 0;
  41.   tt.tv_usec= 0;
  42.   FD_ZERO(&msk);
  43.   FD_SET(sock, &msk);
  44.   switch ( select( sock+1, &msk, (int *)0, (int *)0, &tt )  ) {
  45.   case 0:
  46.   return NOTREADY;
  47.   case -1:
  48.           perror("select");
  49.           exit(1); 
  50.   default:
  51.      if( 0>ioctl( sock, FIONREAD, &sock ) )
  52.            perror("ioctl FIONREAD"), exit(1);
  53.      return sock>0? ISREADY : CLOSED;
  54.   }
  55. }
  56.  
  57. static void client(){
  58. int s;
  59. struct sockaddr_in addr_in;
  60. char hostnm[ 32];
  61. struct  hostent *hent;  
  62. puts("Waiting for server to get ready");
  63. sleep(2); 
  64. if( gethostname( hostnm, 32 ) < 0 ) perror("gethostname"), exit(1);
  65. hent=gethostbyname( hostnm );
  66. if( hent == ( struct hostent *) 0 ) perror(hostnm), exit(1);
  67. s= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  68. if( s < 0) perror("socket"), exit(1);
  69. addr_in.sin_family= AF_INET;
  70. addr_in.sin_port= htons(PORT_NUMBER);
  71. bcopy( hent->h_addr, (char *) &addr_in.sin_addr, hent->h_length );
  72. if( connect( s, &addr_in, sizeof addr_in) < 0 ) 
  73. perror("connect"), exit(1);
  74. if( ioctl( s, FIONBIO, &s ) < 0 ) perror("ioctl FIONBIO"), exit(1);
  75.  while( AsyncCheck( s) != CLOSED ) 
  76.                   puts("Server exists");
  77. puts("Client exiting...");
  78. }
  79.  
  80. static void server() {
  81. int s0, s1;
  82. struct sockaddr_in addr_in;
  83. s0= socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  84. if( s0 < 0) perror("socket"), exit(1);
  85. bzero( (char *) &addr_in, sizeof addr_in);
  86. addr_in.sin_family= AF_INET;
  87. addr_in.sin_port= htons(PORT_NUMBER);
  88. if( bind( s0, &addr_in, sizeof addr_in) < 0 ) perror("bind"), exit(1);
  89. if( listen( s0, SOMAXCONN) < 0 ) perror("listen"), exit(1);
  90. puts("Server is ready");
  91. do 
  92. s1= accept( s0, ( struct sockaddr_in *)0, ( int *)0 );
  93.    while( s1< 0 );
  94. ( void) close( s0);
  95. sleep(6);
  96. puts("Server exiting...");
  97. }
  98.  
  99. void main() {
  100. switch ( fork() ) {
  101.     case 0: server(); 
  102.             exit(0);
  103.     case -1: perror("fork");
  104.              exit(1);
  105.     default : client();
  106.               exit(0);
  107. };
  108. }
  109.  
  110. --- CUT HERE ---
  111.  
  112. best regards
  113.           Saty from Togliatti.
  114. ! iH
  115. -- 
  116.  ******************* Is There Anybody Out There ? **********************
  117.  *                   SATY ( Sergey A. TsYbanov ).                      *
  118.  *  E-mail : saty@ntc.togliatti.su Phone : number +7 (8482) 378-17-53. *
  119.  ***********************************************************************
  120.  
  121.