How to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(C++)?

点点圈 提交于 2019-12-02 03:03:06

问题


I am a newcomer to zeromq. Recently I did some tests on pub/sub of zeromq, and I don't konw how to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ. Hope your help, thank you very much .


回答1:


Learn the basics by working through the examples. For the proxy, just use this, it's from msgqueue.cpp

int main (int argc, char *argv[])
{
    zmq::context_t context(1);
    zmq::socket_t frontend (context, ZMQ_XSUB);
    frontend.bind("tcp://*:5559");
    zmq::socket_t backend (context, ZMQ_XPUB);
    zmq_bind (backend, "tcp://*:5560");
    zmq_proxy (frontend, backend, NULL);
    return 0;
}



回答2:


Proxy:

int main (int argc, char *argv[])
{
zmq::context_t context(1);
zmq::socket_t frontend (context, ZMQ_XSUB);
....//set hwm
frontend.bind("tcp://*:5559");
zmq::socket_t backend (context, ZMQ_XPUB);
....//set hwm
zmq_bind (backend, "tcp://*:5560");
zmq_proxy (frontend, backend, NULL);
return 0;
}

The reason I lost message is that I should have called setsockopt before bind or connect.

Refer to 0MQ API documentation for setsockopt:

Caution: All options, with the exception of ZMQ_SUBSCRIBE, ZMQ_UNSUBSCRIBE and ZMQ_LINGER, only take effect for subsequent socket bind/connects.



来源:https://stackoverflow.com/questions/18570810/how-to-implement-pub-sub-network-with-a-proxy-by-using-xpub-and-xsub-in-zeromqc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!