Reputation: 351
I have the following control flow. In a for loop, I make an array of strings. I send out each row of the array (a string) to a publisher function. The publisher function then is supposed to send out the strings. I have a problem where my subscriber only receives messages, if the publisher is publishing in an infinite loop. Why can I not continuously call the publisher function and have it send data out? Why is it only working in an infinite loop?
Here is my publisher function:
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;
Here is my subscriber function:
#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;
}
Why is that when I take away the while(1) statement, I don't receive anything?
Upvotes: 0
Views: 187
Reputation: 351
So it turns out that this is a classic "slow joiner problem". I made the publisher sleep a bit, and it worked.
Upvotes: 0