tags:

views:

1005

answers:

2

I have an application where I have potentially many processes must send a message to one process. I figured the most expedient way to do this would be to just send a UDP datagram.

When opening a socket, I need to specify which port to listen to. As I just want to fire and forget the UDP datagram, I'd like to just have the OS pick an available port. I'd rather not have to hunt for one, especially as many instances of my process may be running. 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 fire and forget my UDP datagram?

A: 

Answering the "Problem" rather than the "Question"

If all the processes are operating on the same PC, you might want to look into shared files or some other means of communications other than a networking stack.

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

Quick guide 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.

Tom Leys
Files are what I have right now, but I find processes get backed up against the file system at times of heavy traffic.As its an executable that I launch for every visit, big libraries are out, as is anything that requires a lot of overhead to set up.
Matthias Wandel
+3  A: 

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

Jerub
Ah, I figured it was something that simple. Thanks!
Matthias Wandel
Note that you'll still need some mechanism to let the other processes know to which socket they must transmit!
Alnitak
Well, my receiving process has a static socket number. But the sending processes don't expect to get a reply.
Matthias Wandel
in which case your question is confusing - it sounded like you were trying to ask how to pick a port for the receiving process. Your transmitting processes don't need to "listen", they need to "bind".
Alnitak
Senders don't need to bind either, unless you want the traffic to be sent from a specific interface on a multi-homed host.
Bklyn