I have implemented one publisher and one subscriber network using ZeroMQ. I used XPUB and XSUB, because I want the subscriber be able to send data to publisher as well. My code works for XPUB + SUB, meaning subscriber can receive data from publisher. However, it does not work for XPUB + XSUB. This is probably because I can NOT do "setsockopt(ZMQ_SUBSCRIBE, ... )". I always get "Invalid argument" error at run time. Here is my code:
/* Code */
zmq::context_t context (1);
// Socket to talk to server
std::cout << "Collecting updates from weather server...\n" << std::endl;
zmq::socket_t subscriber (context, ZMQ_XSUB);
subscriber.connect("tcp://localhost:5556");
char *filter = "10001 ";
subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
/*
zmq::message_t msg(10);
sprintf ((char *) msg.data(), "%05d %d %d", 1, 2, 3);
std::cout << "To Send from SUB" << std::endl;
subscriber.send(msg);
std::cout << "Send from SUB" << std::endl;
*/
// Process 100 updates
int update_nbr;
long total_temp = 0;
for (update_nbr = 0; update_nbr < 10; update_nbr++) {
zmq::message_t update;
int zipcode, temperature, relhumidity;
subscriber.recv(&update);
std::istringstream iss(static_cast<char*>(update.data()));
iss >> zipcode >> temperature >> relhumidity ;
total_temp += temperature;
}
std::cout << "Average temperature for zipcode '"
<<"' was "<<(int) (total_temp / update_nbr) <<"F"
<< std::endl;
What's wrong in the way I set subscriber.setsockopt? If I replace the XSUB by SUB, it works. However, then I can not do the subscribe.send(msg) in the code.