How to find a locally available UDP port using the Unix Sockets API

I have an application where I potentially have many processes need to send a message to a single process. I decided that the most reasonable way to do this would be to simply send a UDP datagram.

When opening a socket, I need to specify which port to listen on. Since I just want to start and forget the UDP datagram, I would like the OS to choose an available port. I would rather not hunt for it, especially since many instances of my process might work. Is there a way I can tell the OS to just pick an available port (or better yet, not even open a listening socket) to start and forget my UDP datagram?

+1


source to share


2 answers


Yes. Specify 0 for the port. The OS will select an available port for you.



+3


source


Answering "Problem" rather than "Question"

If all processes are running on the same PC, you may want to look at shared files or other means of communication other than the networking stack.

I suggest you explore the options in the Boost Interprocess library http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess.html



A quick guide is here: http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess/quick_guide.html

Specifically, I suggest you use a named pipe or shared memory between your processes.

0


source







All Articles