2014-02-26 54 views
0

我是zeromq新手,我修改了zeromq示例以测试pub-sub模式的行为,订阅者订阅了两个主题“ABC”和“ABD”,一切顺利,但是当我重新启动发布者时,用户端只收到“ABD”。为什么?在zeromq发布商重新启动后订阅丢失

#include "zhelpers.hpp" 

int main() 
{ 
    // Prepare our context and subscriber 
    zmq::context_t context(1); 
    zmq::socket_t subscriber (context, ZMQ_SUB); 
    subscriber.connect("tcp://localhost:5563"); 
    subscriber.setsockopt(ZMQ_SUBSCRIBE, "ABC", 3); 
    subscriber.setsockopt(ZMQ_SUBSCRIBE, "ABD", 3); 

    while (1) { 

      // Read envelope with address 
      std::string address = s_recv (subscriber); 
      // Read message contents 
      std::string contents = s_recv (subscriber); 

      std::cout << "[" << address << "] " << contents << std::endl; 
    } 
    return 0; 
} 

酒馆

#include "zhelpers.hpp" 

int main() 
{ 
    // Prepare our context and publisher 
    zmq::context_t context(1); 
    zmq::socket_t publisher(context, ZMQ_PUB); 
    publisher.bind("tcp://*:5563"); 

    while (1) 
    { 
     // Write two messages, each with an envelope and content 
     s_sendmore (publisher, "ABC"); 
     s_send (publisher, "We don't want to see this"); 
     s_sendmore (publisher, "ABD"); 
     s_send (publisher, "We would like to see this"); 
     sleep (1); 
    } 
    return 0; 
} 

输出

[ABC] We don't want to see this 
[ABD] We would like to see this 
[ABC] We don't want to see this 
[ABD] We would like to see this 
[ABC] We don't want to see this 
[ABD] We would like to see this 
//kill and restart publisher 
[ABD] We would like to see this 
[ABD] We would like to see this 
[ABD] We would like to see this 
[ABD] We would like to see this 
[ABD] We would like to see this 
+0

您正在使用哪个版本的0MQ?我无法用3.2.3重现您的问题。 –

回答

0

我不知道,如果你解决了这个问题,但我面对这种情况了。这是ZMQ的一个问题,它实际上是在版本4.0.4中解决的(请参阅this thread)。

问候。