简体   繁体   中英

ZeroMQ: Python Server to C++ Client

Currently working on a Python to ZMQ Program; this is what I have for the c++ client so far:

    zmq::context_t context{1};
    zmq::socket_t socket{context, zmq::socket_type::req};
    socket.connect("tcp://IPADDRESSPLACEHOLDERTEXT:51000");
    const std::string data{"Message"};
     
    zmq::message_t reply{};
    socket.recv(reply, zmq::recv_flags::none);
    char CHAR[10];
    memcpy(CHAR, zmq_msg_data(&reply), 9);
    CHAR[10] = '\0';

    std::cout << “RESULT: " << CHAR << "," << std::endl;

    return 0;

However, when I run the full-scale Qt program it crashes. ZMQ itself works (tested it with a c++ server, slightly different code) but doesn't work here. Isolated the program thus far to determine that the problem comes from this code block.

Q : ... doesn't work here

A :
Yes,
it cannot work here. ZeroMQ REQ -Scalable-Formal-Communications-Pattern archetype ( typically working "against" its matching counterpart, like in a REQ/REP -channel setup ) can never start waiting to receive an "answer"-message ( typically from a REP -peer, but without ever having asked & delivered a "question"-message before that ). The code hangs waiting for an answer that will never be delivered this way.

Once you repair the code to make the REQ -instance first "ask", with socket.send( aQuestionMESSAGE, zmq::send_flags::none); , before it "waits", be it polling or listening ( be it using a blocking on non-blocking form thereof ), the ZeroMQ will take care of the rest of the "interleaved" two-step dance in the REQ/REP -archetype:
dancing Ask-Listen-Ask-Listen-Ask-Listen-... on the REQ -side,
whereas
dancing Listen-Reply-Listen-Reply-Listen-... on the REP -side

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