Zeromq: How to access tcp message in c++ -
i new-by zeromq , make way through c++ hello-world example of echo client-server pattern (request-reply). server looks like:
// // hello world server in c++ // binds rep socket tcp://*:5555 // expects "hello" client, replies "world" // #include <zmq.hpp> #include <string> #include <iostream> #include <unistd.h> int main () { // prepare our context , socket zmq::context_t context (1); zmq::socket_t socket (context, zmq_rep); socket.bind ("tcp://*:5555"); while (true) { zmq::message_t request; // wait next request client socket.recv (&request); std::cout << "received hello" << std::endl; // 'work' sleep (1); // send reply client zmq::message_t reply (5); memcpy ((void *) reply.data (), "world", 5); socket.send (reply); } return 0; } now question: how can access / read real data socket.recv() ? trying:
std::cout << request << std::endl; resulted in error message:
error: no match ‘operator<<’ in ‘std::operator<< [with _traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&) (& std::cout)), ((const char*)"received hello")) << request’ the same goes client side sending message. don't find way display real message...
the hello world example goes half way , outputs hard-coded values:
std::cout << "received hello" << std::endl; printing actual response can done follows:
zmq::message_t reply; socket.recv (&reply); std::string rpl = std::string(static_cast<char*>(reply.data()), reply.size()); std::cout << rpl << std::endl; there other useful examples in zhelpers.hpp.
Comments
Post a Comment