[zeromq-dev] PUB sent, but SUB can not receive.

Richard_Newton at waters.com Richard_Newton at waters.com
Wed Aug 14 21:25:00 CEST 2013


You will need to serialise it somehow.

We use protocol buffers (from google), but other options are things like thrift (from facebook), message pack or many others.

If you don't want to use any of the existing ones you could hack your own up, but this will get old real quick, something like (untested):

std::string serialised = wd.Symbol + "," + wd.Time;
zmq::message_t msg(serialised.size());
memcpy(msg.data(), serialised.data(), serialised.size());

and on the other side:

std::string serialised((char*)msg.data(), (char*)msg.data() + msg.size());
std::string::size_type commaPos = serialised.find(',');
wd.Symbol = serialised.substr(0, commaPos );
wd.Time= serialised.substr(commaPos +1, serialised.size() - commaPos  - 1);

I may well have messed up the +1 and -1 there, but you get the idea.  You could also use std::stringstream and std::getline to make things easier.

Like I say, this gets old fast so if you have a lot of structures you need to send I'd consider using a real serialisation mechanism.

Ric.




From:	liuchuanbo at gmail.com
To:	"ZeroMQ development list" <zeromq-dev at lists.zeromq.org>,
Cc:	zeromq-dev-bounces at lists.zeromq.org
Date:	14/08/2013 04:27 PM
Subject:	Re: [zeromq-dev] PUB sent, but SUB can not receive.
Sent by:	zeromq-dev-bounces at lists.zeromq.org



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, Richard_Newton at waters.com 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: liuchuanbo at gmail.com
      To: "ZeroMQ development list" <zeromq-dev at lists.zeromq.org>,
      Cc: zeromq-dev-bounces at lists.zeromq.org
      Date: 14/08/2013 01:59 PM
      Subject: Re: [zeromq-dev] PUB sent, but SUB can not receive.
      Sent by: zeromq-dev-bounces at lists.zeromq.org





      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, Richard_Newton at waters.com 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: liuchuanbo at gmail.com
            To: zeromq-dev at lists.zeromq.org,
            Date: 14/08/2013 01:45 PM
            Subject: [zeromq-dev] PUB sent, but SUB can not receive.
            Sent by: zeromq-dev-bounces at lists.zeromq.org




            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
            zeromq-dev at lists.zeromq.org
            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
            zeromq-dev at lists.zeromq.org
            http://lists.zeromq.org/mailman/listinfo/zeromq-dev
      _______________________________________________
      zeromq-dev mailing list
      zeromq-dev at lists.zeromq.org
      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
      zeromq-dev at lists.zeromq.org
      http://lists.zeromq.org/mailman/listinfo/zeromq-dev
_______________________________________________
zeromq-dev mailing list
zeromq-dev at lists.zeromq.org
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.
===========================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130814/bbfc2010/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graycol.gif
Type: image/gif
Size: 105 bytes
Desc: not available
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130814/bbfc2010/attachment.gif>


More information about the zeromq-dev mailing list