[zeromq-dev] Binding to TCP port 0

Martin Lucina martin at lucina.net
Thu Jan 26 01:58:49 CET 2012

On Wed, 25 Jan 2012 18:39:02 -0600
Pieter Hintjens <ph at imatix.com> wrote:

> On Wed, Jan 25, 2012 at 6:19 PM, Martin Lucina <martin at lucina.net> wrote:
> > It is disabled because in ZeroMQ, by design, there is no way to work
> > with individual underlying connections of a socket. Hence, there is no
> > API equivalent to e.g. getsockname() which you could use to get the port
> > back from the OS.
> The functionality regards binding not connections, this isn't the concern.

> > If you can think of a way to make this work, without exposing the
> > details of the individual connections to the application, and
> > without a backward-incompatible API change, please propose it (or
> > even better, make a patch).
> Current working code would not break by returning the port number via
> the zmq_bind API.

How? zmq_bind() does not have an out parameter. You mean abusing the
(int) return value of zmq_bind() to return a port number? That's
horrible and not all transports have such a thing as a port number,
so it's not generic either.

> The other solution, if this is too ugly (which is debatable) is via a
> zmq_getsockopt call. That was your suggestion previously, iirc. Both
> are worth exploring.
> I don't recall any valid technical concerns with this change proposal,
> it came down to opinion.

The major technical problem which everyone (including me) forgets every
time this comes up is that you can bind a single ZeroMQ socket to
multiple endpoints. Cf. what I said above, some of the bound endpoints
may not even have such a thing as a "port number".

To create an API for this you basically end up with some way to
enumerate the bound endpoints and get them to the application. And at
that point we're coming dangerously close to letting people enumerate
connected endpoints, etc.

Martin Lucina <martin at lucina.net>

More information about the zeromq-dev mailing list