发布者只在无限循环中发送消息



我有以下控制流。在for循环中,我创建了一个字符串数组。我将数组的每一行(一个字符串)发送给发布者函数。然后发布者函数应该发送字符串。如果发布者在无限循环中发布,那么我的订阅者只接收消息。为什么我不能连续调用publisher函数并让它发送数据?为什么它只在无限循环中工作?

下面是我的publisher函数:
    int zedMQserver(std::string output)
     {
     //std ::cout << output.c_str()<< std:: endl;


zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
publisher.bind("tcp://*:5556");
publisher.bind("ipc://device.ipc");                // Not usable on Windows.

    int i=1;   
    //while(1) {


    //  Send message to all subscribers
  const int SIZE = output.length();
    zmq::message_t message(SIZE+1);
    snprintf ((char *) message.data(), (SIZE+1) ,
        "%s", output.c_str());
    publisher.send(message);
   std :: cout << "sent message" << std:: endl;
//}
return 0;

这是我的订户函数:

#include <zmq.hpp>
#include <iostream>
#include <sstream>

int main (int argc, char *argv[])

  {
 zmq::context_t context (1);
 //Socket to talk to server
 std::cout << "Collecting updates from device server...n" << std::endl;
 zmq::socket_t subscriber (context, ZMQ_SUB);
 subscriber.connect("tcp://localhost:5556");
 const char *filter = (argc > 1)? argv [1]: "";  // no filter currently
 subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
 //  Process 100 updates
 zmq::message_t update;
 for (int q =0 ; q<1000 ; q++) {
    subscriber.recv(&update);
    std::string output = std::string(static_cast<char*>(update.data()), update.size());

    std:: cout <<output << std:: endl;       
    }  
 return 0;



  }

为什么当我拿走while(1)语句时,我没有收到任何东西?

所以这是一个典型的"slow joiner problem"。我让出版商睡了一会儿,结果奏效了。

最新更新