[zeromq-dev] Binding to TCP port 0

Pieter Hintjens ph at imatix.com
Thu Jan 26 02:28:00 CET 2012

On Wed, Jan 25, 2012 at 6:58 PM, Martin Lucina <martin at lucina.net> wrote:

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

Yes, it's horrid, I didn't claim it was neat :-) But it is technically
compatible since it will not break current working code.

> 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".

Speak for yourself, I did not forget about this :)  Binds, as I said
deliberately, are synchronous. That means you can bind and then
immediately ask for the port number.

Further, 95% of applications bind exactly one time. And further to
that, it is an extreme edge case (one we can ignore IMO) to bind twice
to system-provided ports.

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

Please read Martin's excellent posting about solving the right
problems only and not the full range of theoretical problems. It is
amazing how even experienced engineers can focus so intensely on
solving, perfectly, the wrong problem.

No-one is asking for an API that lets you bind 10 times to port zero
and get all ten results. 100% of use cases here are for binding once
to port zero and getting exactly one result back.

It is bad design process to exaggerate the user's requirements into
absurd extremity and then state there is no simple solution. It's far
wiser to understand the actual problem people are trying to solve, and
solve that simply and minimally.

The elegant and minimal solution is of course to return the system
assigned port via a zmq_getsockopt and I'd make that patch except it's
evening here in Dallas and there's a bar with barely-clad waitresses
that has a cold beer waiting for me.


More information about the zeromq-dev mailing list