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

后端 未结 2 904
-上瘾入骨i
-上瘾入骨i 2021-01-22 22:56

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

2条回答
  •  一整个雨季
    2021-01-22 23:43

    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.

提交回复
热议问题