简体   繁体   中英

ZMQ wait for a message, have client wait for reply

I'm trying to synchronise 4 clients to one server. I want to send a message to the server when the client is ready to move on, then the server counts how many requests it gets and sends a message back to the clients to say it's ready.

What I've done so far is use 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;
    }
}

With more than one camera, this produces the expected error:

Operation cannot be accomplished in current state

Because it cannot do anything until it's replied to the REQ, right?

How can I modify this to work with multiple clients?

EDIT: I have attempted to implement a less strict REQ REP with PUSH PULL. The meat is:

Server:

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));
}

Clients:

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));
}

I have outputted all of the ports and addresses to check that they're set right.

The server program hangs with the output:

... 
waiting
rx 
tx

This means that the program is hanging on the send, but I can't see for the life of me why

EDIT 2: I have made a github repo with a compilable example and linux makefile and converted to use REP REQ again. The issue is that the client doesn't accept the message from the server, but again, I don't know why.

The answer was to use two REP REQ sockets as in edit 2. I had made a stupid typo for "REQ" instead of "REP" in one of the variable usages and hadn't noticed. I was therefore connecting and then binding the same socket.

I will leave the github repo up as I think the question is long enough already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM