ZeroMQ服务器客户端启动顺序

时间:2019-04-03 07:42:05

标签: c++11 zeromq

如果客户端在服务器之前启动,则会遇到问题

规格:带有c ++ 11,libzmq的ubuntu 16.04:4.2.3

问题:示例代码

server.cpp

int main()
{
zmq::context_t context(1);
zmq::socket_t requester(context,ZMQ_ROUTER);
.
//code to get address
.    
requester.bind(address);

while(true)
{
zmq::message_t message;
requester.recv(&message);
.
//remaining code
.
}
return 0;
}

client.cpp

int main()
{
zmq::context_t context(1);
zmq::socket_t requester(context,ZMQ_DEALER);
.
//code to get address
.
requester.connect(address);
zmq::message_t message;
.
//populate the message to send
.

requester.send(message);
return 0;
}

我知道即使在服务器未运行的情况下,我也可以在zmq中启动客户端,但是我的客户端应用程序必须包括一项安全检查,该检查要求启动服务器。其他解决方法。超时选项对我不起作用

1 个答案:

答案 0 :(得分:0)

将发送设为非阻塞,如果没有可用的服务器,发送将失败并设置错误号

http://api.zeromq.org/4-2:zmq-send

ZMQ_DONTWAIT
For socket types (DEALER, PUSH) that block when there are no available peers 
(or all peers have full high-water mark), specifies that the operation should 
be performed in non-blocking mode. If the message cannot be queued on the 
socket, the zmq_send() function shall fail with errno set to EAGAIN.