ZeroMQ Multipart Post Part Two Size 0

I am trying to get a message with several parts using the sub / pub pattern in ZMQ 4.2.3 and cppzmq. I can successfully send and receive messages of individual parts, but when I try to read in the second frame, its size is always 0. What completely confused me is the C # version using NetMQ, which reads the second frame without problems, forcing me to consider that it is sent properly. I know that something is missing for me, but today is my second day trying to find out what is without success.

Here is my pub code

#include <iostream>
#include "zmq_addon.hpp"
void main()
{
    zmq::context_t ctx = zmq::context_t();
    zmq::socket_t pub = zmq::socket_t(ctx, zmq::socket_type::pub);
    try
    {
        //binding using localhost gives an error about invalid device?
        pub.bind("tcp://*:8845");
    }
    catch (...)
    {
        std::cout << zmq_strerror(zmq_errno());
        std::cin.get();
        return;
    }
    byte topic = 8;
    std::string id = "ragefire.bob";
    while (true)
    {
        std::cout << "Spam\n";
        pub.send(id.c_str(), id.length(), ZMQ_SNDMORE);
        pub.send("foo", 3);
    }
}

And my C ++ subcode

#include <iostream>
#include "zmq_addon.hpp"
int main()
{
    zmq::context_t ctx = zmq::context_t();
    zmq::socket_t sub = zmq::socket_t(ctx, zmq::socket_type::sub);
    sub.connect("tcp://localhost:8845");
    std::string id = "ragefire.bob";
    sub.setsockopt(ZMQ_SUBSCRIBE, id.c_str(), id.length());
    while (true)
    {
        zmq::message_t msg;
        if(sub.recv(&msg,ZMQ_NOBLOCK))
        {
            auto rpl = std::string(static_cast<char*>(msg.data()), msg.size());
            std::cout << "Recv returned true! " << rpl << "\n";
            int more;
            auto more_size = sizeof(more);
            sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
            while (more)
            {
                zmq::message_t moreMsg;
                sub.recv(&msg);
                std::string moreRpl = std::string(static_cast<char*>(moreMsg.data()), moreMsg.size());
                std::cout << "There more! " << moreRpl << "Size is " << moreMsg.size() << "\n";
                sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
            }
        }
        else
            std::cout << "Recv returned false! " << zmq_strerror(zmq_errno()) << "\n";
    }
}

With an exit

Recv returned true! ragefire.bob
There more! Size is 0

To complete my NetMQ subsystem, which can read both frames

static void Main()
{
    SubscriberSocket sub = new SubscriberSocket("tcp://localhost:8845");
    sub.SubscribeToAnyTopic();
    while (true)
    {
        NetMQMessage msg = sub.ReceiveMultipartMessage();
        Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg.First.Buffer)}");
        Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg[1].Buffer)}");
    }
}
+4
source share
1 answer

sub.recv() &msg, .

sub.recv() &msg, moreMsg. message_t,

sub.recv(&moreMsg);
+2

Source: https://habr.com/ru/post/1684169/


All Articles