我是 ZeroMQ 的新手,并通过 C++ hello-world 示例回显客户端-服务器模式(请求-回复)。服务器看起来像:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
现在我的问题是:如何访问/读取 socket.recv() 的真实数据?试:
std::cout << request << std::endl;
导致错误消息:
error: no match for ‘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’
发送消息的客户端也是如此。我找不到显示真实信息的方法...