[zeromq-dev] automatically choosing a free TCP port

David Robinson zxvdr.au at gmail.com
Mon Mar 15 12:46:49 CET 2010

Hi all,

I'm looking at using avahi (eg, zeroconf) with a zeromq based
application. I'd like to be able to have zeromq bind to any free port
so that I can announce the service via avahi, but afaict the string
passed to zmq_bind must have a port number. Is there any way to avoid
specifying a TCP port number and just have one automatically assigned?

Apparently you can "just create the socket and call listen() on it.
Then, the IP stack will automatically assign a free port number. Use
getsockname() to query the port number that has been chosen and use
that when registering the Avahi service."  I tried using an address
string of "tcp://lo" without success (eg, zmq_bind (socket,

There is an example at the link below that shows how this is done with
normal sockets:



More information about the zeromq-dev mailing list