Search code examples
c++zeromqmemcpy

ZeroMQ Pub Sending Empty String


I've got a simple C++ PUB and python SUB set up, with intent to have C++ side built as a simple DLL eventually. I've had some prior experience with a similar set up with python on both sides, and no issues. I am, however a total C++ noob.

My C++ code:

#define ZMQ_EXPORT

#include "stdafx.h"
#include "zmq.hpp"

int _tmain(int argc, _TCHAR* argv[]) {
    zmq::context_t context(1);
    zmq::socket_t publisher(context, ZMQ_PUB);
    publisher.bind("tcp://*:6666");

    zmq::message_t message(5);
    memcpy(message.data(), "Hello", 5);

    while(true) {
        Sleep(500);
        publisher.send(message);
    }

    return 0;
}

Result from python SUB script on recv_multipart():

['']

I am confident it is otherwise working, though I think there's a flaw with how I am doing the memcpy.


Solution

  • I'm thinking your missing the whole 'subscription' part of pub/sub

    You need to give the PUB message some sort of message filter. This also means that your SUB needs to do the setsockopt to be able to receive messages.

    You're given example shows that you in fact do not have a message filter for your PUB message (or rather your "Hello" IS your message filter and the data message is infact an empty string).