home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / sun / misc / 5390 < prev    next >
Encoding:
Text File  |  1992-11-22  |  5.6 KB  |  187 lines

  1. Path: sparky!uunet!spool.mu.edu!agate!stanford.edu!rutgers!uwvax!gorgon!fullfeed!erb
  2. From: erb@fullfeed.fullfeed.com (David Erb)
  3. Newsgroups: comp.sys.sun.misc
  4. Subject: problem: listen port not released on socket close...help?
  5. Message-ID: <1992Nov23.012150.14404@fullfeed.fullfeed.com>
  6. Date: 23 Nov 92 01:21:50 GMT
  7. Organization: FullFeed: Public Access Usenet/E-mail at +1 608 246 2701 guest
  8. Lines: 177
  9.  
  10. /*__________________________________________________________________________
  11.  * Dear Netland.
  12.  * Below is a test program to illustrate a problem with sockets.
  13.  * When I run it several times in succession, I get the message:
  14.  *    bind: Address already in use
  15.  * Sometimes I get this message the second time, sometimes not until
  16.  * the tenth.  After getting the message once, I will get the message
  17.  * every time I run the program.  After 5 minutes or so without running
  18.  * the program, the condition seems to clear itself and I can run again.
  19.  * Doing a compile as stated below also seems to clear the condition.
  20.  * After the condition has cleared, it will resurface after I run the
  21.  * program a few times again.
  22.  *
  23.  * Any help would be greatly appreciated.  Please email me direct and
  24.  * I will post a summary if warranted.
  25.  *
  26.  * SunOS 4.1.1 on Sun4/150 running X11R5 with xterm, epoch and xmeter.
  27.  * Compile with either:
  28.  *   cc -o socktest socktest.c
  29.  *   gcc -o socktest socktest.c  (problem more frequent with gcc)
  30.  *
  31.  * program summary:
  32.  * fork
  33.  * if child (server)
  34.  *   socket
  35.  *   bind
  36.  *   listen
  37.  *   accept
  38.  *   read
  39.  *   write
  40.  *   close and close
  41.  * if parent (client)
  42.  *   wait 2 secods for the server to make it to "accept"
  43.  *   gethostbyname
  44.  *   socket
  45.  *   connect
  46.  *   write
  47.  *   read
  48.  *   close
  49.  * Thanks in advance.
  50.  * David Erb, erb@fullfeed.com
  51.  *__________________________________________________________________________*/
  52.  
  53. #include <sys/types.h>
  54. #include <sys/param.h>
  55. #include <sys/stat.h>
  56. #include <sys/wait.h>
  57. #include <sys/socket.h>
  58. #include <netinet/in.h>
  59.  
  60. #include <rpc/xdr.h>
  61.  
  62. #include <fcntl.h>
  63. #include <ctype.h>
  64. #include <stdio.h>
  65. #include <errno.h>
  66. #include <netdb.h>
  67.  
  68. /*__________________________________________________________________________
  69.  *__________________________________________________________________________*/
  70. int
  71. main(argc, argv)
  72.   int argc;
  73.   char *argv[];
  74. {
  75.   exit(socktest(argc, argv));
  76. }
  77.  
  78. int
  79. socktest(argc, argv)
  80.   int argc;
  81.   char *argv[];
  82. {
  83.   int server_sock, client_sock;
  84.   struct sockaddr_in server;
  85.   struct sockaddr_in client;
  86.   struct hostent *hostentry;
  87.   int clientlen;
  88.   int rc, data;
  89.   union wait status;
  90.  
  91.   memset(server, 0, sizeof(server));
  92.   server.sin_port = htons(5702);        /* set service port                 */
  93.   server.sin_family = AF_INET;
  94.  
  95.   rc = fork();
  96.   if (rc == 0)                          /* we are the child?                */
  97.   {                                     /* ====== SERVER ================== */
  98.     server_sock = socket(AF_INET,       /* make the socket                  */
  99.       SOCK_STREAM, 0);
  100.     if (server_sock < 0)
  101.       {perror("server socket"); return(1);}
  102.  
  103.     server.sin_addr.s_addr =            /* host we will talk to             */
  104.       INADDR_ANY;
  105.  
  106.     rc = bind(server_sock,              /* bind server to service port      */
  107.       &server, sizeof(server));
  108.     if (rc < 0)
  109.       {perror("bind"); return(1);}
  110.  
  111.     rc = listen(server_sock, 5);        /* mark socket as a listener        */
  112.     if (rc < 0)
  113.       {perror("listen"); return(1);}
  114.  
  115.     clientlen = sizeof(client);
  116.     client_sock = accept(server_sock,   /* first accept connection          */
  117.       &client, &clientlen);
  118.     if (client_sock < 0)
  119.       {perror("accept"); return(1);}
  120.  
  121.     rc = read(client_sock,              /* read from socket                 */
  122.       &data, sizeof(data));
  123.     if (rc == -1)
  124.       {perror("server read"); return(1);}
  125.  
  126.     printf("server got data %d\n",
  127.       data);
  128.     data = data * data;
  129.  
  130.     rc = write(client_sock,             /* write into socket                */
  131.       &data, sizeof(data));
  132.     if (rc == -1)
  133.       {perror("server write"); return(1);}
  134.  
  135.     close(client_sock);
  136.     close(server_sock);
  137.   }
  138.   else                                  /* ======= CLIENT ================= */
  139.   if (rc > 0)                           /* we are the parent?               */
  140.   {
  141.     sleep(1);                           /* let server get established       */
  142.     server_sock = socket(AF_INET,       /* make socket                      */
  143.       SOCK_STREAM, 0);
  144.     if (server_sock < 0)
  145.       {perror("client socket"); return(1);}
  146.  
  147.     hostentry = gethostbyname(          /* get host entry                   */
  148.       "localhost");
  149.     if (hostentry == 0)
  150.     {perror("gethostbyname"); return(1);}
  151.  
  152.     server.sin_addr.s_addr =            /* host we want to talk to          */
  153.       ((struct in_addr *)
  154.         (hostentry->h_addr))->s_addr;
  155.  
  156.     rc = connect(server_sock,           /* bind local and connect foreign   */
  157.       &server, sizeof(server));
  158.     if (rc < 0)
  159.       {perror("connect"); return(1);}
  160.  
  161.     data = 2;
  162.     rc = write(server_sock,             /* write into socket                */
  163.       &data, sizeof(data));
  164.     if (rc == -1)
  165.       {perror("client write"); return(1);}
  166.  
  167.     rc = read(server_sock,              /* read from socket                 */
  168.       &data, sizeof(data));
  169.     if (rc == -1)
  170.       {perror("client read"); return(1);}
  171.  
  172.     printf("client got data %d\n",
  173.       data);
  174.  
  175.     close(server_sock);                 /* close socket to server           */
  176.  
  177.     rc = wait(&status);                 /* see what child did               */
  178.     if (rc < 0)
  179.       {perror("wait"); return(1);}
  180.   }
  181.   else
  182.   if (rc == -1)                         /* couldn't fork?                   */
  183.     {perror("fork"); return(1);}
  184.  
  185.   return(0);
  186. }
  187.