getsockname does not return local port
I have a simple UDP client program where getsockname appears to be
behaving incorrectly. This is on Solaris 10.
This is more or less what I am doing. You can find many
examples of code like this on the web... I do more error
checking. :-)
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(0);
/*** Create socket on which to send and receive ***/
sd = socket (AF_INET,SOCK_DGRAM,0);
bind( sd, (SA *) &server, sizeof(server) );
/*** get port information and prints it out ***/
length = sizeof(server);
getsockname (sd, (SA *)&server,&length) );
printf("Server Port is: %d\n", ntohs(server.sin_port));
So I am asking for INADDR_ANY and port 0 initially, which
means "give me any port".
The problem is getsockname is returning 0, which causes
a problem in my application.
Has anyone seen this, or can anyone guess what might
cause it?
I *do* check the return from getsockname, and it is NOT returning
an error.
Thanks,
Mitch
Re: getsockname does not return local port
On Jul 4, 1:32 am, "mitch...@yahoo.com" <mitch...@yahoo.com> wrote:[color=blue]
> I have a simple UDP client program where getsockname appears to be
> behaving incorrectly. This is on Solaris 10.
>
> This is more or less what I am doing. You can find many
> examples of code like this on the web... I do more error
> checking. :-)
>
> struct sockaddr_in server;
>
> server.sin_family = AF_INET;
> server.sin_addr.s_addr = htonl(INADDR_ANY);
> server.sin_port = htons(0);
>
> /*** Create socket on which to send and receive ***/
> sd = socket (AF_INET,SOCK_DGRAM,0);
>
> bind( sd, (SA *) &server, sizeof(server) );
>
> /*** get port information and prints it out ***/
> length = sizeof(server);
> getsockname (sd, (SA *)&server,&length) );
>
> printf("Server Port is: %d\n", ntohs(server.sin_port));
>
> So I am asking for INADDR_ANY and port 0 initially, which
> means "give me any port".
>
> The problem is getsockname is returning 0, which causes
> a problem in my application.
>
> Has anyone seen this, or can anyone guess what might
> cause it?
>
> I *do* check the return from getsockname, and it is NOT returning
> an error.
>
> Thanks,
>
> Mitch[/color]
Hi Mitch,
There seems to be no error in your code. However you can check the
following article and copy paste the example program and verify if it
works for you.
[url]http://www.techpulp.com/articles/networking/snip-find-local-addr-using-getsockname.php[/url]
It works well in Linux. I don't see any reason why it shouldn't work
in Solaris.
- Neo
Techpulp Technologies