Solved

Getting port number of socket after bind with sin_port=0

Posted on 2004-04-09
12
2,711 Views
Last Modified: 2012-05-04
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
Comment
Question by:iemonslice
  • 7
  • 5
12 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 100 total points
ID: 10793551
Hi iemonslice,

Call getsockname().

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


Good Luck,
Kent
0
 

Author Comment

by:iemonslice
ID: 10793668
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
 
LVL 45

Expert Comment

by:Kdo
ID: 10793732
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:iemonslice
ID: 10793765
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
 
LVL 45

Expert Comment

by:Kdo
ID: 10793791
Hi iemonslice,

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

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


0
 

Author Comment

by:iemonslice
ID: 10793933
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
 
LVL 45

Expert Comment

by:Kdo
ID: 10793977
Hi iemonslice,

Print the address that is in 'me'.



0
 

Author Comment

by:iemonslice
ID: 10794102
Do you mean me.sin_addr.s_addr? If so, printing it out right after the bind outputs 0.
0
 

Author Comment

by:iemonslice
ID: 10797421
Anyone have any ideas about this problem of mine?
0
 

Author Comment

by:iemonslice
ID: 10797764
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
 
LVL 45

Expert Comment

by:Kdo
ID: 10798131
Cool.

Where does this stand now?
Kent
0
 

Author Comment

by:iemonslice
ID: 10798223
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 85
Arduino EDI - Programming language 3 89
What is atomic operation? 6 58
How to access ANSI/IEEE Std 754 or equivalent information ? 3 63
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now