[zeromq-dev] SO_REUSEADDR behavior on Windows

Chris Trimble trimbo at gmail.com
Tue Jun 29 18:29:26 CEST 2010


Try this pyzmq script on Windows and Linux:

>>> import zmq
>>> c = zmq.Context()
>>> s1 = c.socket(zmq.PUB)
>>> s1.bind("tcp://*:47000")
>>> s2 = c.socket(zmq.PUB)
>>> s2.bind("tcp://*:47000")

On linux, you'll get an error that the address is already in use.  On
Windows, there's no error.

Looks like this is because SO_REUSEADDR has a different behavior on Windows,
and is still being set as a sockopt.  MS says this:

"In the case where the first call to
bind<http://msdn.microsoft.com/en-us/library/ms737550(v=VS.85).aspx>
sets
> either SO_REUSEADDR or no socket options at all, the second bind call will
> "hijack" the port and the application will be unable to determine which of
> the two sockets received specific packets sent to the "shared" port."


http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx

Is this the intended behavior of ZMQ?  I'm not sure it will be determinate.

Looks like all that needs to be done to line up the behavior on windows is
change the sockopt to SO_EXCLUSIVEADDRUSE on line 67 of tcp_listener.cpp,
which is inside the windows #ifdef

 - Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100629/7efb1054/attachment.html>


More information about the zeromq-dev mailing list