I can only find old C++ source examples. Anyways, I did mine, based on them. Here's my publisher in python:
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5563")
while True:
msg = "hello"
socket.send_string(msg)
print("sent "+ msg)
sleep(5)
And here's the subscriber in C++:
void * ctx = zmq_ctx_new();
void * subscriber = zmq_socket(ctx, ZMQ_SUB);
// zmq_connect(subscriber, "tcp://*:5563");
zmq_connect(subscriber, "tcp://localhost:5563");
// zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", sizeof(""));
while (true) {
zmq_msg_t msg;
int rc;
rc = zmq_msg_init( & msg);
assert(rc == 0);
std::cout << "waiting for message..." << std::endl;
rc = zmq_msg_recv( & msg, subscriber, 0);
assert(rc == 1);
std::cout << "received: " << (char * ) zmq_msg_data( & msg) << std::endl;
zmq_msg_close( & msg);
}
Initially, I tried zmq_setsockopt( subscriber, ZMQ_SUBSCRIBE, "", sizeof("") );
but I guess I should receive everything if I don't set this, right? So I commented it.
When I run the code, I see "waiting for message..." forever.
I tried to listen to TCP traffic using tcpdump
. Turns out that when the publisher is turned on, I see a lot of garbage on the 5563
port, and when I turn the publisher off, they stop. When I tried a PUSH/PULL
scheme, I could see the plaintext message in tcpdump
. (I tried pushing with nodejs and pulling with c++ and it worked).
What could I be doing wrong?
I tried different combinations of .bind()
, .connect()
, localhost
, 127.0.0.1
, but they won't work either.
UPDATE: I've just read that I must subscribe to something, so I did zmq_setsockopt( subscriber, ZMQ_SUBSCRIBE, NULL, 0 );
to subscribe to everything but I still receive nothing
PyZMQ is in version 17.0.0.b3 and has ZeroMQ 4.2.3
C++ has ZeroMQ 4.2.2
UPDATE 2:
Updates both to 4.2.3, won't work either.