getsockname()
David Schwartz
davids at webmaster.com
Wed Jan 7 13:41:03 PST 2004
> (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
More information about the freebsd-stable
mailing list