C++ Zeromq如何制作c++;允许它不等待客户端请求的服务器程序
我正在尝试服务器和客户端的Zeromq Hello world示例。下面是示例代码C++ Zeromq如何制作c++;允许它不等待客户端请求的服务器程序,c++,sockets,zeromq,akka-zeromq,C++,Sockets,Zeromq,Akka Zeromq,我正在尝试服务器和客户端的Zeromq Hello world示例。下面是示例代码 // // Hello World client in C++ // Connects REQ socket to tcp://localhost:5555 // Sends "Hello" to server, expects "World" back // #include <zmq.hpp> #include <string> #include <iostream>
//
// Hello World client in C++
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
#include <zmq.hpp>
#include <string>
#include <iostream>
int main ()
{
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
std::cout << "Connecting to hello world server…" << std::endl;
socket.connect ("tcp://localhost:5555");
// Do 10 requests, waiting each time for a response
for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
zmq::message_t request (6);
memcpy ((void *) request.data (), "Hello", 5);
std::cout << "Sending Hello " << request_nbr << "…" << std::endl;
socket.send (request);
// Get the reply.
zmq::message_t reply;
socket.recv (&reply);
std::cout << "Received World " << request_nbr << std::endl;
}
return 0;
}
//
C/C++世界客户端
//将REQ套接字连接到tcp://localhost:5555
//向服务器发送“Hello”,期望返回“World”
//
#包括
#包括
#包括
int main()
{
//准备上下文和套接字
zmq::context\u t context(1);
套接字(上下文,zmq_-REQ);
std::cout您需要使用线程来完成此操作。
以Boost线程为例
您可以尝试使用ZMQ\u PUSH/ZMQ\u PULL
组合,使客户端请求无阻塞
如果您想超越hello world,您可能想看看的体系结构。让服务器继续运行是什么意思?您是指多线程吗?很可能您不想这样做。如果您想将应用程序扩展到许多客户端,您可能应该签出
服务器套接字将在后台自动异步收集消息。您通常会在服务器的一个位置串行收集消息,并根据您的意愿(异步或非异步)将响应发送给消息以进行进一步处理。您可能想了解非阻塞套接字,t但我不知道如何为ZMQ启用该功能。
//
// 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;
}