home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!batcomputer!ghost.dsi.unimi.it!newsserver.di.unipi.it!lopi
- From: lopi@cli.di.unipi.it (Lopi)
- Subject: How Unbind a Socket ??
- Message-ID: <lopi.722256706@cli>
- Keywords: socket bind
- Sender: netnews@di.unipi.it (USENET News System)
- Nntp-Posting-Host: dipisa.di.unipi.it
- Organization: Dipartimento di Informatica, Universita' di Pisa
- Date: Fri, 20 Nov 1992 10:51:46 GMT
- Lines: 101
-
- hi,
-
- I'm trying some experiences with sockets , connections and similar things...
- I create a socket with the socket() call, I bind it , listen and accept
- a connection ( made using 'telnet host port' ) then I allow both the parts
- to write something in a very rudimental talk.
- The problem is closing the connection, I use shutdown() for both the sockets
- given by socket() and accept(), but the name of the first socket remains
- reserved for a certain time and I cannot run again the program until system
- frees the first socket... (errno 48 : address already in use) this happens
- only when I close the connection, if
- is 'telnet' to close ,obviously, all works properly and I soon can run again
- the prg.
- I think that shutdown doesn't suffice to destruct the socket, but manuals
- don't talk about any other system calls.
- Does anybody knows anything about that ?
-
- Fabio Lopiano.
-
- lopiano@athena.di.unipi.it
- lopiano@cli.di.unipi.it
- lopi@dipisa.di.unipi.it
-
- P.S.
- Please answer on this group, I'm not sure that mail works properly on my host...
-
- code follows :
-
- ------------------8<--------------------8<---------------------8<---------------
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <errno.h>
-
- main() {
- int s,ns;
-
- struct sockaddr_in addr,raddr;
- int rlen;
- int i;
- int b,l;
-
- addr.sin_family = AF_INET;
- addr.sin_port = 1999;
- addr.sin_addr.s_net = 131;
- addr.sin_addr.s_host = 114;
- addr.sin_addr.s_lh = 4;
- addr.sin_addr.s_impno = 38;
-
- s = socket( AF_INET , SOCK_STREAM , 6 );
- if( s<0 ) exit();
- b = bind( s, &addr , sizeof(addr) );
- if( b<0 ) {
- printf("Err n. : %d\n",errno);
- printf("Try later...\n");
- shutdown(s);
- exit();
- }
- l = listen( s , 5 );
- printf("Let's wait....\n");
- ns = accept( s , &raddr , &rlen);
- printf("Ok!\n");
- if( fork() ) connection_1(ns);
- else connection_2(ns);
- shutdown(ns);
- shutdown(s);
- }
-
- #define SEND(s) write( sock, s, strlen(s) )
- #define GET(c) read( sock, &c, 1 )
- #define PUT(c) write( sock, &c, 1 )
-
- connection_1(sock) /* remote -> local (father) */
- int sock;
- {
- char c;
- SEND( "connected to Athena.di.unipi.it\n");
- SEND( "please, write something ... \n");
- SEND( "> ");
- while( c != '*' ){
- GET(c);
- printf("%c",c);
- if(c=='\n') SEND( "> ");
- }
- printf("Connection closed .\n");
- }
- connection_2(sock) /* local -> remote (son) */
- int sock;
- {
- char c;
- printf("please write something ... \n> ");
- while( c!= '*'){
- c=getchar();
- PUT(c);
- if(c=='\n') printf("> ");
- }
- }
-
-
-