- 
                Notifications
    You must be signed in to change notification settings 
- Fork 792
Closed
Description
I'm having an issue using cppzmq to send a message from a ROUTER to a DEALER using a prepended ID to route to the specific dealer. I've constructed a minimal example below:
router.cpp
#include <iostream>
#include <zmq.hpp>
#include <string>
#include <thread>
#include <chrono>
int main() {
    zmq::context_t context;
    zmq::socket_t socket (context, zmq::socket_type::router);
    socket.setsockopt(ZMQ_ROUTER_MANDATORY, 1);
    socket.bind("tcp://*:5555");
   getchar() ;  // Wait till we launch dealer.cpp, then we'll hit enter to send the message
    try {
        std::string jobRequest = "ExampleJobRequest";
        // Send JobRequest to camera to run
        std::cout << "Router: Sending msg: " << jobRequest << std::endl;
        // Set the address, then the empty delimiter and then the request itself
        socket.send("PEER2", ZMQ_SNDMORE);
        //socket.send(zmq::message_t(), ZMQ_SNDMORE);
        socket.send(zmq::str_buffer("ExampleJobRequest"));
        
        // Receive the reply from the camera
        std::cout << "Router: Waiting for reply from camera " << std::endl;
        zmq::message_t reply;
        socket.recv(&reply);
        std::cout << "Router: Received " <<  std::string(static_cast<char*>(reply.data()), reply.size()) << std::endl;
    } catch (std::exception e) {
        std::cout << "Router Error: " << e.what();
    }
    std::this_thread::sleep_for(std::chrono::seconds(1));
    //socket.setsockopt(ZMQ_LINGER, 0);
    socket.close();
    context.close();
}dealer.cpp
int main (void)
{
    //  Prepare our context and socket
    zmq::context_t context;
    zmq::socket_t socket (context, zmq::socket_type::dealer);
    std::cout << "Dealer: Connecting to RunJob server… \n";
    // Identity of peer socket must be set before binding/connecting
    socket.setsockopt(ZMQ_IDENTITY, "PEER2", 5);
    socket.connect ("tcp://localhost:5555");
    while(true) {
        try {
            // Wait for next request from client
            std::cout << "Dealer: Waiting for request" << std::endl;
            zmq::message_t request;
            zmq::message_t empty;
            // Receive request
            socket.recv(&request);
            std::string requestString = std::string(static_cast<char*>(request.data()), request.size());
            std::cout << "Dealer: Received request" << std::endl;
            std::cout << requestString << std::endl;
            socket.send(zmq::str_buffer("Job completed"), zmq::send_flags::dontwait);
        }catch (std::exception e) {
            std::cout << "Router Error: " << e.what();
        }
    }
    // Used to set various 0MQ Socket Settings
    // ZMQ_Linger - Set linger period for socket shutdown
    socket.setsockopt(ZMQ_LINGER, 0);
    socket.close();
    context.close();
    return 0;
}After executing these two programs, they both hang on the recv calls. I have search extensively for the cause of this solution. I've also outlines this issue in a stackoverflow post. It appears that the message that the ROUTER is sending to the DEALER is getting lost, and not sent correctly.
Metadata
Metadata
Assignees
Labels
No labels