为什么jeromq使用setReuseAddress(true)? [英] why does jeromq use setReuseAddress(true)?

查看:159
本文介绍了为什么jeromq使用setReuseAddress(true)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是zeromq的新手,而不是使用套接字的人.

I'm new to zeromq and not that experienced with sockets.

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

Are ZeroMQ sockets supposed to only allow one socket to bind() to a port?

jeromq实现允许一个以上的实现; pyzmq没有.谁是对的?

The jeromq implementation allows more than one; pyzmq does not. Who's correct?

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

The jeromq ZMQ.Socket.bind() function eventually comes down to this:

https://github.com. 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

推荐答案

jeromq实现允许多个

The jeromq implementation allows more than one

不,不是.在TCP套接字上设置重用地址只能解决以下常见的开发问题:在与应用程序先前实例的连接仍处于TIME_WAIT状态的情况下,无法绑定侦听套接字.不允许端口的两个实例处于LISTEN状态.

No it doesn't. Setting reuse-address on a TCP socket only fixes the frequent development problem that a listening socket can't be bound while there are connections to a prior instance of the application still in the TIME_WAIT state. It doesn't allow two instances of the port to be in LISTEN state.

这篇关于为什么jeromq使用setReuseAddress(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆