Search code examples
c++zeromq

How to send integer values between server and client using zeromq(zmq)?


I have a C++ server and client code with zmq ( ZeroMQ ) lib. I would like to send integer values to the client.

I have read through the manual for zmq, but I have a problem sending anything else than a char array from the server to client.

Can anyone help me with the code? Thanks in advance.


Solution

  • You just need a message format.

    Size efficient would be a struct:

    Send side

    struct Message
    {
      uint32_t one;
      uint64_t two;
    };
    
    Message msg;
    msg.one = 1;
    msg.two = 20;
    send(&msg, sizeof(Message));
    

    Or if you want something more flexible you could send json etc

    Receive side:

    assuming you use cppzmq headers

    Message* msg;
    zmq::message_t zmsg;
    sock->recv(&zmsg);
    msg = (Message*)zmsg.data();
    int one - msg->one;