Date: Wed, 7 Jan 2004 13:40:54 -0800 From: "David Schwartz" <davids@webmaster.com> To: "Christian Klein" <chris@schwer.bewaff.net>, <freebsd-stable@freebsd.org> Subject: RE: getsockname() Message-ID: <MDEHLPKNGKAHNMBLJOLKKEOIJEAA.davids@webmaster.com> In-Reply-To: <91136089-4152-11D8-942B-000393076EEC@schwer.bewaff.net>
next in thread | previous in thread | raw e-mail | index | archive | help
> (this is of course not my program, just some code that does the same) > > --- snipp --- > #include <stdio.h> > #include <sys/types.h> > #include <sys/socket.h> > #include <string.h> > #include <stdlib.h> > #include <netinet/in.h> > #include <arpa/inet.h> > > int main() > { > int sock, len; Okay, you haven't set the value of 'len'. > struct sockaddr_in addr, foo; > > if((sock=socket(AF_INET, SOCK_STREAM, 0))<0) > { > exit(0); > } > > memset(&addr, 0, sizeof(struct sockaddr_in)); > addr.sin_family = AF_INET; > addr.sin_addr.s_addr = INADDR_ANY; > addr.sin_port = htons(0); > > if(bind(sock, (struct sockaddr *) &addr, sizeof(struct > sockaddr_in))<0) > { > perror("bind"); > exit(0); > } > > if(listen(sock, 5)<0) > { > perror("listen"); > exit(0); > } > > getsockname(sock, (struct sockaddr *) &foo, &len); Hmm, you still haven't set the value of 'len'. And you ignore the return value of 'getsockname'. > fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), > ntohs(foo.sin_port)); > > return 0; > } > $ ./a.out > listening on 86.186.4.40:49087 > $ ./a.out > listening on 0.0.0.0:3810 Fix those two bugs and see if you still have a problem. DS
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?MDEHLPKNGKAHNMBLJOLKKEOIJEAA.davids>