【发布时间】:2018-03-07 13:27:37
【问题描述】:
在我的代码中,我只能从我连接的第一个发布者(端口 5556)接收消息。
那么在连接到第二个(5557)之前,我需要关闭第一个连接(5556)吗?
如果是,那么在ZeroMQ指南的声明中
“订阅者可以连接到多个发布者,每次使用一个连接调用。然后数据将到达并被交错(“公平排队”),因此没有一个发布者会淹没其他发布者。”
短语“每次使用一个连接调用”是否意味着我们需要在连接到第二个发布者之前关闭第一个连接?
如何同时连接到多个发布者以接收来自两者的消息。
代码:
#include <zmq.hpp>
#include
int main (int argc, char *argv[])
{
zmq::context_t context (1);
zmq::socket_t subscriber (context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.connect("tcp://localhost:5557");
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);//subscribe to all messages
// Process 10 updates
int update_nbr;
for (update_nbr = 0; update_nbr < 10 ; update_nbr++) {
zmq::message_t update;
subscriber.recv (&update);
// Prints only the data from publisher bound to port 5556
std::string updt = std::string(static_cast<char*>(update.data()), update.size());
std::cout << "Received Update/Messages/TaskList " << update_nbr <<" : "<< updt << std::endl;
}
return 0;
}
【问题讨论】:
标签: c++ zeromq publish-subscribe