getsockname does not return local port - TCP-IP

This is a discussion on getsockname does not return local port - TCP-IP ; 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... ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: getsockname does not return local port

  1. 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



  2. Re: getsockname does not return local port

    On Jul 4, 1:32 am, "mitch...@yahoo.com" wrote:
    > 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


    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.

    http://www.techpulp.com/articles/net...etsockname.php

    It works well in Linux. I don't see any reason why it shouldn't work
    in Solaris.

    - Neo
    Techpulp Technologies

+ Reply to Thread