我正在使用 ZeroMQ 和 NACK-Oriented Reliable Multicast (NORM) norm://
协议(protocol)。该文档仅包含一段 Python 代码,所以这是我的 C++ 代码:
PUB
发件人:
string sendHost = "norm://2,127.0.0.1:5556";// <NormNodeId>,<addr:port>
string tag = "MyTag";
string sentMessage = "HelloWorld";
string fullMessage = tag + sentMessage;
zmq::context_t *context = new zmq::context_t( 20 );
zmq::socket_t publisher( *context, ZMQ_PUB );
zmq_connect( publisher, sendHost.c_str() );
zmq_send( publisher,
fullMessage.c_str(),
fullMessage.size(),
0
);
SUB
接收者:
char message[256];
string receiveHost = "norm://1,127.0.0.1:5556";// <NormNodeId>,<addr:port>
string tag = "MyTag";
zmq::context_t *context = new zmq::context_t( 20 );
zmq::socket_t subscriber( *context, ZMQ_SUB );
zmq_bind( subscriber, receiveHost.c_str() );
zmq_setsockopt( subscriber, ZMQ_SUBSCRIBE, tag.c_str(), tag.size() );
zmq_recv( subscriber,
message,
256,
0
);
cout << bytesReceived << endl;
cout << message << endl;
我面临的问题是,根据 documentation .bind()
和 .connect()
可以互换。
在我的例子中,它们都执行了一个.bind()
,这导致 ZeroMQ 抛出一个错误,指出第二个绑定(bind)失败,因为地址已经在使用错误。
最佳答案
... they both do a bind, which causes ZeroMQ to throw an error saying the second bind fails
是的,这是失败的正确状态。
第一个 .bind()
“拥有”端口,这是一个排他性角色。
的互换性 { .bind() | .connect() }
应该理解,哪一边都没有关系.bind()
-s 和哪个 .connect()
-s.
直到这一刻,我还没有看到有人以双方都会尝试的方式解释此属性 .connect()
(一个不存在的 .bind()
-(未)暴露的接入点),越少尝试 .bind()
一个已经“占用”的端口(如果驻留在同一个本地主机上),或者保持在 nox-et-solitudo 状态,对于 .bind()
中的任何一个的情况-s 建立这样一个 .connect()
- 不同 localhost-s 上的两个端口都处于就绪状态,此后两者都保持沉默(永远),因为没有(并且将)没有尝试进行任何 .connect()
-ion 上线并运行。
不,您只需要 1
.bind()
,从那一刻起可能会处理 0+
future .connect()
-请求,到达建立直播 channel PUB/SUB
, 对于任何相应的 <transport-class>
协议(protocol),包括新增的 norm://
.
无论如何,欢迎 norm://
到 ZeroMQ 协议(protocol)系列。
困惑?
可以享受再阅读 5 秒
关于中的主要概念差异 [ ZeroMQ hierarchy in less than a five seconds ] 或其他帖子和讨论 here .
关于c++ - ZeroMQ with NORM - address already in use error was thrown on 2nd .bind() - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50753588/