home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / programm / 5373 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.9 KB  |  114 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!batcomputer!ghost.dsi.unimi.it!newsserver.di.unipi.it!lopi
  3. From: lopi@cli.di.unipi.it (Lopi)
  4. Subject: How Unbind a Socket ??
  5. Message-ID: <lopi.722256706@cli>
  6. Keywords: socket bind
  7. Sender: netnews@di.unipi.it (USENET News System)
  8. Nntp-Posting-Host: dipisa.di.unipi.it
  9. Organization: Dipartimento di Informatica, Universita' di Pisa
  10. Date: Fri, 20 Nov 1992 10:51:46 GMT
  11. Lines: 101
  12.  
  13. hi,
  14.  
  15. I'm trying some experiences with sockets , connections and similar things...
  16. I create a socket with the socket() call, I bind it , listen and accept
  17. a connection ( made using 'telnet host port' ) then I allow both the parts
  18. to write something in a very rudimental talk.
  19. The problem is closing the connection, I use shutdown() for both the sockets
  20. given by socket() and accept(), but the name of the first socket remains
  21. reserved for a certain time and I cannot run again the program until system
  22. frees the first socket... (errno 48 : address already in use) this happens
  23. only when I close the connection, if
  24. is 'telnet' to close ,obviously, all works properly and I soon can run again
  25. the prg. 
  26. I think that shutdown doesn't suffice to destruct the socket, but manuals
  27. don't talk about any other system calls.
  28. Does anybody knows anything about that ?
  29.  
  30. Fabio Lopiano.
  31.  
  32. lopiano@athena.di.unipi.it
  33. lopiano@cli.di.unipi.it 
  34.    lopi@dipisa.di.unipi.it
  35.  
  36. P.S.
  37. Please answer on this group, I'm not sure that mail works properly on my host...
  38.  
  39. code follows :
  40.  
  41. ------------------8<--------------------8<---------------------8<---------------
  42.  
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <netinet/in.h>
  46. #include <stdio.h>
  47. #include <errno.h>
  48.  
  49. main() {
  50. int s,ns;
  51.  
  52.     struct sockaddr_in addr,raddr;
  53.     int rlen;
  54.     int i;
  55.     int b,l;
  56.     
  57.     addr.sin_family     = AF_INET;
  58.     addr.sin_port       = 1999;
  59.     addr.sin_addr.s_net    = 131;
  60.     addr.sin_addr.s_host    = 114;
  61.     addr.sin_addr.s_lh    = 4;
  62.     addr.sin_addr.s_impno    = 38;
  63.     
  64.     s = socket( AF_INET , SOCK_STREAM , 6 );
  65.     if( s<0 ) exit();
  66.     b = bind( s, &addr , sizeof(addr) );
  67.     if( b<0 ) { 
  68.         printf("Err n. : %d\n",errno);
  69.         printf("Try later...\n");
  70.         shutdown(s);
  71.         exit();
  72.         }
  73.     l = listen( s , 5 );
  74.     printf("Let's wait....\n");
  75.     ns = accept( s , &raddr , &rlen);
  76.     printf("Ok!\n");
  77.     if( fork() ) connection_1(ns);
  78.         else connection_2(ns);
  79.     shutdown(ns);
  80.     shutdown(s);
  81. }
  82.  
  83. #define SEND(s)    write( sock, s, strlen(s) )
  84. #define GET(c) read( sock, &c, 1 )
  85. #define PUT(c) write( sock, &c, 1 )
  86.  
  87. connection_1(sock)    /* remote -> local     (father) */
  88. int sock;
  89. {
  90.     char c;
  91.     SEND( "connected to Athena.di.unipi.it\n");
  92.     SEND( "please, write something ... \n");
  93.     SEND( "> ");
  94.     while( c != '*' ){
  95.         GET(c);
  96.         printf("%c",c);
  97.         if(c=='\n') SEND( "> ");
  98.     }    
  99.     printf("Connection closed .\n");
  100. }
  101. connection_2(sock)    /* local -> remote     (son) */
  102. int sock;
  103. {
  104.     char c;
  105.     printf("please write something ... \n> ");
  106.     while( c!= '*'){
  107.         c=getchar();
  108.         PUT(c);     
  109.         if(c=='\n') printf("> ");
  110.     }
  111. }
  112.  
  113.  
  114.