From owner-freebsd-hackers Sat Sep 15 17:53:51 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from beppo.feral.com (beppo.feral.com [192.67.166.79]) by hub.freebsd.org (Postfix) with ESMTP id 8610D37B405 for ; Sat, 15 Sep 2001 17:53:47 -0700 (PDT) Received: from mailhost.feral.com (mjacob@mailhost.feral.com [192.67.166.1]) by beppo.feral.com (8.11.3/8.11.3) with ESMTP id f8G0riI11246; Sat, 15 Sep 2001 17:53:45 -0700 (PDT) (envelope-from mjacob@feral.com) Date: Sat, 15 Sep 2001 17:53:44 -0700 (PDT) From: Matthew Jacob X-Sender: mjacob@beppo Reply-To: mjacob@feral.com To: Stephen Montgomery-Smith Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: Could not bind In-Reply-To: <3BA3F70D.27C2136@math.missouri.edu> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG You should always use perror or strerror if a system call fails so you know *why* it fails. Then you can cross-references with the man(2) and man(3) pages to figure out what's up. On Sat, 15 Sep 2001, Stephen Montgomery-Smith wrote: > I have written a server program that listens on port 3000. The program > works very well except for one feature. I am asking if that is normal, > or whether I forgot something. > > If I run the program it does fine. If I then kill the program (after it > has accepted connections), and then run the program again, the bind > function fails to work, and I get a message like "Could not bind" (see > program below). If I wait a while, like a minute or two, then the > program will work again. Is this normal behavior, or did I miss > something? > > I got the programming style from Richard Steven's book on network > programming. The structure of the program is something like this: > > > typedef struct { > int connfd; > struct in_addr addr; > u_short port; > } > arg_pass_type; > > void *process_client(void *arg) { > int connfd = ((arg_pass_type*)arg)->connfd; > struct in_addr addr = ((arg_pass_type*)arg)->addr; > u_short port = ((arg_pass_type*)arg)->port; > > free(arg); > pthread_detach(pthread_self()); > do_lots_of_stuff(); > > } > > int main () { > int listenfd; > struct sockaddr_in servaddr; > socklen_t slen; > pthread_t tid; > arg_pass_type *arg; > > listenfd=socket(AF_INET,SOCK_STREAM,0); > bzero(&servaddr,sizeof(servaddr)); > servaddr.sin_family=AF_INET; > servaddr.sin_addr.s_addr=htonl(INADDR_ANY); > servaddr.sin_port=htons(3000); > if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0) > { > fprintf(stderr,"Could not bind\n"); > exit(1); > } > listen(listenfd,6); > > while (1) > { > slen=sizeof(servaddr); > arg = malloc(sizeof(arg_pass_type)); > arg->connfd = accept(listenfd,(struct sockaddr*)&servaddr,&slen); > arg->addr = servaddr.sin_addr; > arg->port = servaddr.sin_port; > pthread_create(&tid,NULL,process_client,(void*) arg); > } > exit(0); > } > > -- > Stephen Montgomery-Smith > stephen@math.missouri.edu > http://www.math.missouri.edu/~stephen > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message