std::istringstream can be constructed with a std::string, and
std::string can be constructed with a char* AND length. So:
std::istringstream iss(static_cast<char*>(ticker.data()));
becomes
std::istringstream iss(std::string(static_cast<char*>(ticker.data()),
ticker.size()));
On 04/08/13 15:51, Joshua Tacoma wrote:
> Roy,
>
> I believe what's going on is the istringstream doesn't know where the
> string ends because the frame doesn't end with a '\0'. Except when it
> does by coincidence, which might be often enough that you get 1 a lot of
> the time.
>
> I don't know the best C++ idiom for this - maybe there's a way to limit
> the number of bytes the istringstream will read to the number of bytes
> in the stream?
>
> On Sun, Aug 04, 2013 at 09:43:43PM +0800, Roy Liu wrote:
>> 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?
>> _______________________________________________
>> zeromq-dev mailing list
>> [email protected]
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> [email protected]
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev