我是ZeroMQ的新手,并通过回显客户端 - 服务器模式(Request-Reply)的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;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题:如何访问/读取socket.recv()的真实数据?试:
std::cout << request << std::endl;
Run Code Online (Sandbox Code Playgroud)
导致错误消息:
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’
Run Code Online (Sandbox Code Playgroud)
发送消息的客户端也是如此.我找不到显示真实信息的方法......
Nik*_*lia 41
hello world示例只进行了一半并输出了硬编码值:
std::cout << "Received Hello" << std::endl;
Run Code Online (Sandbox Code Playgroud)
打印实际响应可以按如下方式进行:
zmq::message_t reply;
socket.recv (&reply);
std::string rpl = std::string(static_cast<char*>(reply.data()), reply.size());
std::cout << rpl << std::endl;
Run Code Online (Sandbox Code Playgroud)
zhelpers.hpp中还有一些其他有用的示例.