ZeroMQ C ++ req-rep终止错误

时间:2013-04-29 07:54:41

标签: c++ tcp zeromq

这是简单请求的ZeroMq C ++代码 - 回复两者交替消息。 但是,只有连续发送消息时才会发生错误....

回复代码:

include zmq.hpp
include string
include iostream
include unistd.h
include ctime

    int main () {
// Prepare our context and socket
  zmq::context_t context (1);
  zmq::socket_t socket (context, ZMQ_REP);
  socket.bind ("tcp://*:5557");
  zmq::message_t reply (5);

  memcpy ((void *) reply.data (), "World", 5);
  zmq::message_t request;

// Wait for next request from client
  socket.recv (&request);
///////////////// """observe_line_1"""
  std::cout << "Received Hello" << std::endl;
  while (true)
  {
 // Send reply back to client
    socket.send (reply);
  }
  return 0;
}

请求代码:

include zmq.hpp
include string
include iostream
include ctime

int main ()
{
  zmq::context_t context (1);
  zmq::socket_t socket (context, ZMQ_REQ);

  socket.connect ("tcp://localhost:5557");
  zmq::message_t request (6);
  memcpy ((void *) request.data (), "Hello", 5);
  zmq::message_t reply;

  socket.send(request);
//////////////////////////////////////////"""observe_line_2"""
   for (int request_nbr = 0; request_nbr != 1000; request_nbr++) 
   {
    socket.recv (&reply);
    std::cout << "Received World " << request_nbr << std::endl;
   }
   return 0;
}

输出 执行Reqply然后执行请求然后发生跟随错误

terminate called after throwing an instance of 'zmq::error_t'
  what():  Operation cannot be accomplished in current state
Aborted

指出要点

当“observe_line_1”在它下面的for循环内部时,“observe_line_2”在里面,而在它下面循环并执行,那么代码会成功执行而没有任何错误????

1 个答案:

答案 0 :(得分:4)

REQ-REP模型不允许为一个请求发送多个回复。

  

REQ-REP套接字对是锁定的。客户端在一个循环中发出zmq_send()然后发出zmq_recv()(如果需要,则发出一次)。执行任何其他序列(例如,连续发送两个消息)将导致发送或recv调用的返回码为-1。同样,服务按此顺序按顺序发出zmq_recv()和zmq_send()。

referencealternative socket typesmaybe this can help