I have a simple ZeroMQ request reply pattern, that I am trying to implement as two threads. However When I run the below code, I get the following error.
C:\Users\karthik\Documents\Workspace\cpluspluspen\cmake-build-debug\zeromq\zmq_ex1\zmq_server.exeWaiting on receive()connect to 5555terminate called after throwing an instance of 'zmq::error_t' what(): Invalid argumentProcess finished with exit code 0
What am I doing wrong?
#include <zmq.hpp>#include <iostream>#include <unistd.h>#include <future>class ZmqServer{public: ZmqServer() : m_context(1), m_replySocket(m_context, ZMQ_REP), m_requestSocket(m_context, ZMQ_REQ) { } void Bind(std::string protocol ,int port){ std::string bind_param; bind_param = protocol +"://*:" + std::to_string(port); m_replySocket.bind(bind_param); } void receive(){ zmq::message_t request; // Wait for next request from client m_replySocket.recv (&request); std::cout << "Received message : " << request << std::endl; } void send(std::string msg){ // Send reply back to client zmq::message_t reply (msg.size()); memcpy (reply.data (), msg.c_str(), 5); m_replySocket.send (reply); } void connect(std::string protocol, int port) { std::string bind_param; bind_param = protocol +"://*:" +"localhost" +std::to_string(port); m_requestSocket.connect(bind_param); }private: zmq::context_t m_context; zmq::socket_t m_replySocket; zmq::socket_t m_requestSocket;};void serverThread(std::shared_ptr<ZmqServer> zmq){ std::cout << "Waiting on receive()" << std::endl; sleep(1); zmq->receive(); sleep(1); zmq->send("World");}void clientThread(std::shared_ptr<ZmqServer> zmq){ std::cout << "connect to 5555" << std::endl; zmq->connect("tcp", 5555); zmq->send("Hello"); sleep(1); zmq->receive();}int main () { auto zmq = std::make_shared<ZmqServer>(); zmq->Bind("tcp", 5555); auto sThread = std::thread( serverThread, std::ref(zmq)); auto cThread = std::thread(clientThread, std::ref(zmq)); cThread.join(); sThread.join(); return 0;}