Thread: How to make a UDP server socket with an ephemeral port.

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    How to make a UDP server socket with an ephemeral port.

    Hello,

    I am building a system that will use a custom directory service to store communication information (process, host, port, etc.) between processes that are programmed in C. Because of this, none of my UDP server applications will need to start up and bind to a well-known port; they can use ephemeral ones. However, I must inform my directory service what that ephemeral port is once I have it. I know you can do this in TCP communications by not binding the port, but how would one go about this in UDP?

    More so, once the applications has obtained the ephemeral port from the kernel, can I then call getsockname() to find out exactly what it is, so that I can send this information to my directory service?

    I hope I was able to state what I'm trying to do clearly enough. Thanks for any help!

    - Marvin

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    If there's less than ten or so ports...I would do something like this, and I'm hoping you're not asking for homework help:

    Code:
    	register int8_t num_ports = 10;
    	register int8_t c;
    	int *p = calloc(1, num_ports * sizeof(int));
    	
    	p[0] = 333;              /* just assign your ports, w/e */
    	p[3] = 56565;
    	p[4] = 1000;
    	p[5] = 100000;
    	p[6] = 61;
    	p[9] = 3;
    
    
    	p[num_ports] = 1;	/* using 1 as an escape character, but you won't use port 1, right...? */
    	
    	for (c = 0; c < num_ports; c++) {
    
    
    		while ( *p == 0) {
    			p++;  
    		}
    		if (*p == 1) break;
    		
    		/* bind your port w/e */
    		
    		*p = 0;
    	}

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Thanks for the reply. Nah, this isn't for homework. I'm just trying to learn if there is a simple way to obtain an ephemeral port for a UDP Server Socket. The example you provided assumes knowledge of the ephemeral port range that whatever OS you are using implements. Can I not just perform a bind(), using an address structure with a port of 0, and have the system assign a port number for me? Then, to acquire what that port number is, I assume I would just call a getsockname() on the socket. I just wondered if anyone knew off the top of their head, but I will whip up a test program soon and see what happens. Thanks for the help!

    - Marvin

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by marvin.carlisle View Post
    Thanks for the reply. Nah, this isn't for homework. I'm just trying to learn if there is a simple way to obtain an ephemeral port for a UDP Server Socket.
    That's not what "ephemeral ports" are for. The server needs to use a pre-defined port so that clients know where to connect to. There are 65000+ ports available, not even 0.1% of them are in use on your machine, I promise. If the port is in use, you will be told, and bind will fail, so there is no risk of conflicts. If you are really paranoid, you can always choose based on this list:

    List of TCP and UDP port numbers - Wikipedia, the free encyclopedia

    Notice there is a "Dynamic, private or ephemeral port" range there. Just pick a number from that and hard code it. Much easier.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-30-2010, 06:52 PM
  2. C# Socket Listen On Specific Port
    By pobri19 in forum C# Programming
    Replies: 2
    Last Post: 05-25-2010, 06:01 AM
  3. Socket Destination Port Problem
    By radeberger in forum Networking/Device Communication
    Replies: 15
    Last Post: 03-26-2009, 11:00 AM
  4. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  5. Socket Server
    By carrotcake1029 in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2008, 11:46 AM