Ric,

I have already had a try.

I use google protocol buffer as a serialisation tool. It's OK. String type is 
not the problem yet.

Steps:
1. serialise the struct
2. send message
3. deserialize the struct


On Aug 14, 2013, at 11:27 PM, [email protected] wrote:

> Ric,
> 
> I have tried it. You're exactly right!
> 
> OS is the Windows here.
> Actually, WData.Symbol is all less than 16 characters, and WData.Time is all 
> more than 16 characters(like 2013-08-08 10:00:00).
> When I set WData.Symbol to more than 16 chars manually, pointer failed.
> 
> Please tell me how to resolve this thing?
> 
> BTW, the WData struct can not be changed. 
> 
> Roy.
> 
> On Aug 14, 2013, at 10:36 PM, [email protected] wrote:
> 
>> Is this on windows?
>> 
>> If so the stl implementation on widows (and clangs implementation, but not 
>> gcc) uses something called the small string optimisation, any string less 
>> than or equal to 16 characters will be stored within the string class itself 
>> so will copy over, anything more than 16 characters will be allocated on the 
>> heap and string will keep a pointer to it, this will not be copied over.
>> 
>> So try a symbol with more than 16 characters and it will probably fail.
>> 
>> Ric.
>> 
>> 
>> <graycol.gif>liuchuanbo---14/08/2013 01:59:43 PM---Hi, Ric Right, string is 
>> std::string.
>> 
>> From:        [email protected]
>> To:  "ZeroMQ development list" <[email protected]>, 
>> Cc:  [email protected]
>> Date:        14/08/2013 01:59 PM
>> Subject:     Re: [zeromq-dev] PUB sent, but SUB can not receive.
>> Sent by:     [email protected]
>> 
>> 
>> 
>> Hi, Ric
>> 
>> Right, string is std::string.
>> Why Symbol(also std::string) has the correct string and Time NOT?
>> 
>> I'm debugging now, WData.Time is ""(a empty string).
>> 
>> On Aug 14, 2013, at 8:54 PM, [email protected] wrote:
>> 
>> Is string here a std::string?  If so you cannot do this as std::string uses 
>> more memory internally,
>> 
>> You'll need either a struct that uses just data arrays like:
>> 
>> 
>> typedef struct tag_WData
>> {
>> charTime[100];
>> char Symbol[100];
>> } WData;
>> 
>> Or use some serialisation mechanism such as protocol buffers.
>> 
>> Ric.
>> 
>> 
>> 
>> <graycol.gif>liuchuanbo---14/08/2013 01:45:26 PM---Hi, everyone My problem 
>> is as the following:
>> 
>> From: [email protected]
>> To: [email protected], 
>> Date: 14/08/2013 01:45 PM
>> Subject: [zeromq-dev] PUB sent, but SUB can not receive.
>> Sent by: [email protected]
>> 
>> 
>> 
>> 
>> Hi, everyone
>> My problem is as the following:
>> 
>> typedef struct tag_WData
>> {
>> string Time;
>> string Symbol;
>> } WData;
>> 
>> 
>> On the PUB side. I can get WData.Time.
>> 
>> void OnData(WData wd)
>> {
>> // PUB Socket send ticker message to subscriber
>> int msgSize = sizeof(WData);
>> zmq::message_t message(msgSize);
>> memcpy ((void *) message.data(), &wd, msgSize);
>> 
>> publisher.send(message);
>> }
>> 
>> 
>> However, on the SUB side, it's very wired that I can NOT get WData.Time, but 
>> WData.Symbol is correct.
>> 
>> int main (int argc, char *argv[])
>> {
>> zmq::context_t context (1); // only 1 io_threads
>> zmq::socket_t subscriber (context, ZMQ_SUB);
>> subscriber.connect("tcp://localhost:5556");
>> 
>> subscriber.setsockopt(ZMQ_SUBSCRIBE, NULL, 0); 
>> 
>> WData *msg;
>> while(true){
>> zmq::message_t message(sizeof(WData));
>> subscriber.recv(&message);
>> msg = (WData *) message.data(); 
>> 
>> // it's ERROR here. error message: Bad ptr
>> cout << msg->Time << endl; 
>> }
>> 
>> return 0;
>> }
>> 
>> Why?
>> Thanks!
>> 
>> _______________________________________________
>> zeromq-dev mailing list
>> [email protected]
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>> 
>> 
>> ===========================================================
>> The information in this email is confidential, and is intended solely for 
>> the addressee(s). 
>> Access to this email by anyone else is unauthorized and therefore 
>> prohibited.  If you are 
>> not the intended recipient you are notified that disclosing, copying, 
>> distributing or taking 
>> any action in reliance on the contents of this information is strictly 
>> prohibited and may be unlawful.
>> ===========================================================
>> 
>> _______________________________________________
>> 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
>> 
>> 
>> ===========================================================
>> The information in this email is confidential, and is intended solely for 
>> the addressee(s). 
>> Access to this email by anyone else is unauthorized and therefore 
>> prohibited.  If you are 
>> not the intended recipient you are notified that disclosing, copying, 
>> distributing or taking 
>> any action in reliance on the contents of this information is strictly 
>> prohibited and may be unlawful.
>> ===========================================================
>> 
>> _______________________________________________
>> 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

Reply via email to