• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2893
  • Last Modified:

Getting port number of socket after bind with sin_port=0

How do I retrieve the random port number after I do a bind on a socket?

    me.sin_family = AF_INET;
    me.sin_addr.s_addr = htonl(INADDR_ANY);
    me.sin_port = htons(0);
    rc = bind(sd, (struct sockaddr *) &me, sizeof(me))

    printf("Sending on port %d\n", random_port);

In other words, how do I get random_port?
0
iemonslice
Asked:
iemonslice
  • 7
  • 5
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi iemonslice,

Call getsockname().

getsockname() will store the assigned address and port into the sockaddr structure."


Good Luck,
Kent
0
 
iemonsliceAuthor Commented:
When I do a getsockname(), I get the following error:

getsockname: Bad address

What does the above mean?

Below is the code segment if you need to see it:
===============================================
    if (getsockname(sd, (struct sockaddr *) &me, (int *)sizeof(me)) == -1){
        perror("getsockname");
      exit(1);
    }
    printf("Listening on port %d\n", me.sin_port);
===============================================
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi iemonslice,

Based on your first call, you need to pass 'rc', not 'sd'.

    if (getsockname(rc, (struct sockaddr *) &me, (int *)sizeof(me)) == -1){
        perror("getsockname");
     exit(1);
    }
    printf("Listening on port %d\n", me.sin_port);


Kent
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
iemonsliceAuthor Commented:
I thought rc was the correct value to pass in, but when I tried it earlier, it gave me an error:

getsockname: Socket operation on non-socket

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi iemonslice,

Hmm.....   My mistake.  Your code should take this sequence:

  SocketId = socket ();
  bind (SocketId, ...);
  getsockname (SocketId, ...);


0
 
iemonsliceAuthor Commented:
That's the way I have it right now. I'll paste the section in it's entirety below so it's easier than having to look above.

With the below I still have the getsockname: Bad address error.
 
===============================================
    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
        perror("socket");
      exit(1);
    }

    me.sin_family = AF_INET;
    me.sin_addr.s_addr = htonl(INADDR_ANY);
    me.sin_port = htons(0);
    if ((bind(sd, (struct sockaddr *) &me, sizeof(me))) == -1){
        perror("bind");
        exit(1);
    }

    if (getsockname(sd, (struct sockaddr *) &me, (int *)sizeof(me)) == -1){
        perror("getsockname");
      exit(1);
    }
===============================================
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi iemonslice,

Print the address that is in 'me'.



0
 
iemonsliceAuthor Commented:
Do you mean me.sin_addr.s_addr? If so, printing it out right after the bind outputs 0.
0
 
iemonsliceAuthor Commented:
Anyone have any ideas about this problem of mine?
0
 
iemonsliceAuthor Commented:
Ah the problem was the last parameter of getsockname().

Instead of sizeof(me), a pointer to the size worked.

socklen_t slen = sizeof(me);
getsock..(x,x,&slent);
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Cool.

Where does this stand now?
Kent
0
 
iemonsliceAuthor Commented:
I actually knew about getsockname() before posting on experts-exchange, but going back to my first post, I never mentioned getsockname() and my problem. You did tell me the correct answer so you get the points. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now