1

I'm sorry for this kinda stupid question, but I didn't find any other answer. How can I send a message from ZMQ_DEALER to ZMQ_REP?

There is server code:

 std::string ans;
 zmq::context_t context;
 zmq::socket_t socket(context, ZMQ_DEALER);
 int port = bind_socket(socket);
 std::cout<<port<<std::endl;
 std::cout<<"sending\n";
 send_message(socket,"test");
 std::cout<<"SUCCESS\n";
 std::cout<<"trying to get msg from client...\n";
 ans=receive_message(socket);
 std::cout<<"TOTAL SUCCESS\n";
 std::cout<<ans<<std::endl;
 close(port);

and there is client code:

    zmq::context_t context;
    zmq::socket_t socket(context, ZMQ_REP);
    std::string recv;
    recv=receive_message(socket);
    std::cout<<" total successss\n";
    send_message(socket,"success");
    std::cout<<recv<<std::endl;

Client can't receive message from server. I tried to find something in official ZeroMQ book, and I found this:

"When a ZMQ_DEALER socket is connected to a ZMQ_REP socket each message sent must consist of an empty message part, the delimiter, followed by one or more body parts."

user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    ZMQ is notorious for its many language bindings and its examples: https://zguide.zeromq.org. Pick one of those to start from. That said, please take the [tour] and read [ask]. The point is, you seem to have code already, but you don't even describe a problem with that, so it's unclear what your problem is. That said, your English is fine, don't worry about that. – Ulrich Eckhardt Dec 27 '21 at 12:05
  • Providing a [mre] would be very helpful for you to get an answer. – zkoza Jan 04 '22 at 09:43

1 Answers1

0

As demanded by the ZeroMQ documentation, the sender has to take care of following the API requirement.

This should meet the need:

#include <string>
#include <zmq.hpp>

int main()
{
   zmq::context_t aCTX;
// ----------------------------------------------------------------- Context() instance
   zmq::socket_t aDemoSOCKET( aCTX, zmq::socket_type::dealer);
// ----------------------------------------------------------------- ZeroMQ Archetype
   aDemoSOCKET.bind( "inproc://DEMO" );
// ----------------------------------------------------------------- ZeroMQ I/F

   const std::string_view   BLANK_FRAME_MSG = "";
   const std::string_view PAYLOAD_FRAME_MSG = "Hello, world ...";

   ...
   aDemoSOCKET.send( zmq::buffer(   BLANK_FRAME_MSG ), zmq::send_flags::sndmore  ); //[*]
   aDemoSOCKET.send( zmq::buffer( PAYLOAD_FRAME_MSG ), zmq::send_flags::dontwait );
   ...
}

The API-requested empty frame is the trick [*], enforced by the flags-parameter in the c-API. There is no more magic behind this. If in doubts, feel free to seek further in many real-world helpful answers here

user3666197
  • 1
  • 6
  • 50
  • 92