[zeromq-dev] why PUB-SUB pattern is not stable?

Roy Liu liuchuanbo at gmail.com
Sun Aug 4 15:43:43 CEST 2013


Hi,

I used PUB-SUB pattern to implement a data distribution.

code as the following:

1. PUB
zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
zmq::message_t message(1);

void OnDataRcv()
{
        _snprintf ((char *) message.data(), 1, "%d", 1);
 publisher.send(message);
}


2.SUB
zmq::context_t context (1);
zmq::socket_t subscriber (context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5556");
subscriber.setsockopt(ZMQ_SUBSCRIBE, NULL, 0);

int msg;
while(true){
zmq::message_t ticker;
 subscriber.recv(&ticker);
std::istringstream iss(static_cast<char*>(ticker.data()));
iss >> msg;
 std::cout << msg << std::endl;
}

In SUB, I often get msg as "-858993460". It looks like a pointer error.
But occasionally, I can get the right msg as "1".
This is why?
Anyone can help me?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130804/94fc6d29/attachment.htm>


More information about the zeromq-dev mailing list