2

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.

4

1 回答 1

2

而不是使用 setsockopt 进行订阅,您需要使用 send 并且消息的第一个字节应该是 1。

在您的情况下,发送过滤器变量,但在过滤器的开头附加一个字节,值为 1(不是 ascci '1',只是 1)。

于 2014-07-06T16:25:46.120 回答