为什么jeromq使用setReuseAddress(true)?

时间:2014-03-10 21:51:38

标签: java sockets zeromq jeromq

我是zeromq的新手,而不是套接字的经验。

ZeroMQ套接字是否应该只允许一个套接字绑定()到一个端口?

jeromq实现允许多个; pyzmq没有。谁是对的?

jeromq ZMQ.Socket.bind()函数最终归结为:

https://github.com/zeromq/jeromq/blob/master/src/main/java/zmq/TcpListener.java#L141

//  Set address to listen on.
public int set_address(final String addr_)  {
    address.resolve(addr_, options.ipv4only > 0 ? true : false);

    try {
        handle = ServerSocketChannel.open();
        handle.configureBlocking(false);
        handle.socket().setReuseAddress(true);
        handle.socket().bind(address.address(), options.backlog);
        if (address.getPort()==0)
            address.updatePort(handle.socket().getLocalPort());
    } catch (IOException e) {
        close ();
        return ZError.EADDRINUSE;
    }
    endpoint = address.toString();
    socket.event_listening(endpoint, handle);
    return 0;
}

的Python:

C:\tmp\jeromq\jeromq-0.3.2\target>python
Python 2.7.5 |Anaconda 1.9.1 (64-bit)| (default, May 31 2013, 10:45:37) [MSC v.1
500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>> ctx=zmq.Context()
>>> s=ctx.socket(zmq.PUB)
>>> s.bind_to_random_port('tcp://127.0.0.1')
56356
>>> s2=ctx.socket(zmq.PUB)
>>> s2.bind('tcp://127.0.0.1:56356')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "socket.pyx", line 465, in zmq.core.socket.Socket.bind (zmq\core\socket.c
:4749)
zmq.core.error.ZMQError: Address in use

1 个答案:

答案 0 :(得分:1)

  

jeromq实现允许多个

不,它没有。在TCP套接字上设置reuse-address只能修复在仍然处于TIME_WAIT状态的应用程序的先前实例的连接时,监听套接字无法绑定的频繁开发问题。它不允许端口的两个实例处于LISTEN状态。