[zeromq-dev] How to receive byte array in ZeroMQ C++

Mykola Ostrovskyy spambox03 at mail.ru
Fri Nov 24 08:27:07 CET 2017


Ravi,

That starts to look less like ZMQ API question and more like a lack of C++
knowledge. The message_t::data() template method gives you a pointer of
specified type.

unsigned char* byteptr = msg.data<unsigned char>();

You can either use it, while ensuring that message object is not destroyed,
or copy the data into another buffer. Since the length of the array is not
known at compile time, you have a number of options:

1. Variable length array (if your compiler supports that)
    unsigned char bytearray[msg.size()];
    std::copy(byteptr, byteptr + msg.size(), bytearray);

2. Dynamically allocated array (don't forget to delete that when you done)
    unsigned char* bytearray = new unsigned char [msg.size()];
    std::copy(byteptr, byteptr + msg.size(), bytearray);

3. Vector
    std::vector<unsigned char> bytearray {byteptr, byteptr + msg.size()};


http://www.letmegooglethat.com/?q=c%2B%2B+pointer+vs+array

Regards,
Mykola



2017-11-24 7:39 GMT+02:00 Ravi Joshi via zeromq-dev <
zeromq-dev at lists.zeromq.org>:

> Dear Mykola,
>
> Thank you very much.
>
> The message is defined in C# as follows:
>
> var message = new ZMessage
> {
>     new ZFrame(myByteArray)
> };
> publisher.Send(message);
>
> I want to get the byte array in C++. May I use something like following:
>
> zmq::message_t msg;
> int rc = zmq_socket.recv(&msg);
> if (rc)
> {
>     unsigned char bytearray[] = msg.data();
> }
>
> Please check out following Pastebin links, to see publisher and subscriber
> code:
> (1) https://pastebin.com/PJ7f2TJ5
> (2) https://pastebin.com/JKf0cZjh
>
>
> -
> Thanks
> Ravi
>
> On Friday, 24 November 2017 1:35 AM, Mykola Ostrovskyy via zeromq-dev <
> zeromq-dev at lists.zeromq.org> wrote:
>
>
>
> Hello Ravi,
>
>
> If that's just a raw byte array, msg.data<uint8_t>() should to the trick.
> For example:
>
>     for (size_t i = 0; i < msg.size(); ++i) {
>         printf("%02X ", msg.data<uint8_t>()[i]);
>     }
>
>
> Regards,
> Mykola
>
>
>
> 2017-11-23 14:52 GMT+02:00 Ravi Joshi via zeromq-dev <
> zeromq-dev at lists.zeromq.org>:
>
> Hello,
> >
> >Hope you are having a good time. I am publishing byte array using ZeroMQ
> in C#. Please see the publisher here (https://pastebin.com/PJ7f2TJ5 )
> >
> >I am trying to receive this byte array in C++. Please see the subscriber
> in C++ here (https://pastebin.com/JKf0cZjh )
> >
> >I want to know that how to receive the sent bytes in C++?
> >
> >-
> >
> >Thanks
> >Ravi
> >______________________________ _________________
> >zeromq-dev mailing list
> >zeromq-dev at lists.zeromq.org
> >https://lists.zeromq.org/ mailman/listinfo/zeromq-dev
> >
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20171124/62b63831/attachment.htm>


More information about the zeromq-dev mailing list