ZMQ等待消息,有客户端等待回复
问题描述:
我试图将4个客户端同步到一台服务器。当客户机准备好继续时,我想发送一条消息给服务器,然后服务器会计算它获得的请求数量,并将消息发送回客户端以表示它已准备好。ZMQ等待消息,有客户端等待回复
什么我目前所做的就是利用REQ/REP:
while(1){
int responses = 0;
while(responses<numberOfCameras){
for(int i=0; i<numberOfCameras;i++){
cout<<"waiting"<<endl;
if(sockets[i]->recv(requests[i], ZMQ_NOBLOCK)){
responses ++;
cout<<"rx"<<endl;
}
}
}
for(int i=0; i<numberOfCameras;i++){
cout<<"tx"<<endl;
sockets[i]->send("k",1);
cout<<"Sent"<<endl;
}
}
使用多台相机,这将产生预期的错误:
Operation cannot be accomplished in current state
因为它不能做任何事情,直到它的回复REQ,对吧?
我该如何修改它以与多个客户端一起工作?
编辑: 我试图用push push来实现一个不太严格的REQ REP。肉:
服务器:
while(1){
int responses = 0;
while(responses<numberOfCameras){
for(int i=0; i<numberOfCameras;i++){
cout<<"waiting"<<endl;
if(REQSockets[i]->recv(requests[i], ZMQ_NOBLOCK)){
responses ++;
cout<<"rx"<<endl;
}
}
}
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
for(int i=0; i<numberOfCameras;i++){
cout<<"tx"<<endl;
REPSockets[i]->send("k",1);
cout<<"Sent"<<endl;
}
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
}
客户:
for (;;) {
std::cout << "Requesting permission to capture"<< std::endl;
REQSocket.send ("?", 1);
// Get the reply.
zmq::message_t reply;
REPSocket.recv (&reply);
std::cout << "Grabbed a frame" << std::endl;
boost::this_thread::sleep(boost::posix_time::seconds(2));
}
我已经输出的所有端口和地址,以检查它们设置正确。
服务器程序与输出挂起:
...
waiting
rx
tx
这意味着程序挂在送,但我不能看到我的生活为什么
编辑2: 我编写了一个带有可编译示例和linux makefile的github repo,并再次转换为使用REP REQ。问题是客户端不接受来自服务器的消息,但是我不知道为什么。
答
答案是在编辑2中使用两个REP REQ套接字。我在其中一个变量用法中为“REQ”而不是“REP”做了一个愚蠢的错字,并没有注意到。因此,我正在连接并绑定相同的套接字。
我会离开github回购,因为我认为这个问题已经够长了。
很高兴你知道了:)请标记你的回答,以便社区其他人知道你的问题已经解决。 – Jason 2015-03-10 19:00:55