c++ - ZMQ Publish/Subscribe pattern publisher connects to subscribers -
i new zmq , did tutorials publish subscribe pattern. application don't quite apply. have 2 types of applications. application 1 can create connections multiple "applications two"s on network , send them data.
i tried implementing publish/subscribe pattern, instead of subscriber connecting publisher publisher connects subscribers.
publisher:
zmq::context_t context(1); zmq::socket_t socket(context, zmq_pub); socket.connect("tcp://localhost:5555"); std::string text = "hello world"; zmq::message_t message(text.size()); memcpy(message.data(), text.c_str(), text.size()); socket.send(message);
subscriber:
zmq::context_t context(1); zmq::socket_t socket(context, zmq_sub); socket.bind("tcp://*:5555"); const char* filter = "hello "; socket.setsockopt(zmq_subscribe, filter, strlen(filter)); zmq::message_t request; socket.recv(&request); std::string message = std::string(static_cast<char*>(request.data()), request.size()); std::cout << "message received!" << std::endl; std::cout << message << std::endl;
the publisher finnishes without error, subscriber stuck in recv(). , yes starting them in right order (subscriber first)
i found solution myself: problem is, publisher send message, before subscriber ready receive. simple
zmq_sleep(1)
before
socket.send(message);
did job.
Comments
Post a Comment