Open smaillns opened 4 years ago
#include <zmq.hpp>
#include <string>
#include <iostream>
void service_function() {
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::string request_str(static_cast<char*>(request.data()), request.size());
std::cout << "Received Request: " << request_str << std::endl;
// Do some 'work'
std::string response = "Service Response to '" + request_str + "'";
// Send reply back to client
zmq::message_t reply(response.size());
memcpy(reply.data(), response.data(), response.size());
socket.send(reply, zmq::send_flags::none);
}
}
void client_function() {
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REQ);
socket.connect("tcp://localhost:5555");
// Create a request
std::string request_str = "Hello";
zmq::message_t request(request_str.size());
memcpy(request.data(), request_str.data(), request_str.size());
// Send the request
std::cout << "Sending request: " << request_str << std::endl;
socket.send(request, zmq::send_flags::none);
// Get the reply
zmq::message_t reply;
socket.recv(reply);
std::string reply_str(static_cast<char*>(reply.data()), reply.size());
std::cout << "Received reply: " << reply_str << std::endl;
}
int main() {
// Run the service in a separate thread or process
std::thread service_thread(service_function);
// Simulate client requests
client_function();
service_thread.join();
return 0;
}
Have you any comment or idea about how to implement such sample https://github.com/smaillns/ZMQ_Samples/blob/master/capture.png Thanks so much
Originally posted by @smaillns in https://github.com/zeromq/libzmq/issues/3583#issuecomment-512134958