I'm trying to write a c++ class for communicating between two computers via ZeroMQ.
To be able to handle errors I am trying to read the return values of the .recv()-
and .send()-
methods but I get the following error
error: cannot convert 'zmq::send_result_t' {aka 'zmq::detail::trivial_optional<unsigned int>'} to 'int' in assignment ret = msocket.send(reply, zmq::send_flags::none);
The code looks like this:
Publisher::Publisher(dataHandler & mdatahandler) :datahandler(mdatahandler)
{
// construct a REP (reply) socket and bind to interface
socketState.bind("tcp://*:5555");
//socketAngles.bind("tcp://*:5556");
//socketCurrents.bind("tcp://*:5557");
}
Publisher::~Publisher()
{
socketState.close();
//socketAngles.close();
//socketCurrents.close();
}
std::string Publisher::transfer(zmq::socket_t& msocket, std::string replyString,
int receiveFlag = 0)
{
zmq::send_result_t ret = 0;
if (receiveFlag)
{
zmq::message_t receivedData;
ret = msocket.recv(receivedData, zmq::recv_flags::none);
if (verbose)
{
std::cout << "Received " << receivedData.to_string() << std::endl;
}
return receivedData.to_string();
}
zmq::message_t reply{ replyString.cbegin(), replyString.cend() };
// send the reply to the client
ret = msocket.send(reply, zmq::send_flags::none);
if (ret == -1)
{
std::cout << zmq_strerror(errno) << std::endl;
}
}
the socket is defined as
zmq::context_t context{ 1 };
zmq::socket_t socketState{ context, ZMQ_REP };
How can I reliably catch errors and is there a better way of handling errors if they occur?
Edit:
I added the zmq::send_result_t
but how can I do anything with it? I can't compare it to anything and I can't print it either.