1

In a C++ ZMQ Publisher I am packing a string up in a message_t like so:

int main()
{
     zmq::context_t context(1);
     zmq::socket_t socket(context, ZMQ_PUB);
     socket.bind("tcp://*:5555");
     socket.setsockopt(ZMQ_CONFLATE, 1);
     std::cout << "Server Up and Sending\n";
     while(true)
     {
          std::string str = "Hello from C++!";
          zmq::message_t msg(str.data(), str.length());
          bool ret = socket.send(msg);
          if(ret)
          {
               std::cout << "Sending\n";
          }
     }
     return 0;
}

I am trying to figure out how to receive in JeroMQ (Java ZMQ); here's what I have so far:

  ZMQ.Context context = ZMQ.context(1);
  ZMQ.Socket socket = context.socket(ZMQ.SUB);
  socket.connect("tcp://localhost:5555");
  socket.subscribe("".getBytes());
  while(true)
  {
        String msg = socket.recvStr();
        System.out.println(msg);
  }

Also in PyZMQ (Python ZMQ):

import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
address = 'tcp://localhost:5555'
socket.connect(address)
socket.setsockopt_string(zmq.SUBSCRIBE, unicode(''))

print "start"
print "connecting to ", address

while True:
    try:
        msg = socket.recv_string()
        print msg

However, neither my JeroMQ attempt or my PyZMQ attempt receive my string message.

Paul McElroy
  • 373
  • 1
  • 2
  • 13
  • The JeroMQ version is missing a `zmq_setsockopt(socket, ZMQ_SUBSCRIBE)` (no idea what the correct syntax is). Perhaps doing `socket.subscribe` does the same, I don't know. – Henri Menke Jun 29 '17 at 23:54
  • I believe `socket.subscribe("".getBytes());` is the JeroMQ equivalent to the CPPZMQ: `socket.setsockopt(ZMQ_SUBSCRIBE, "", 0);` The PyZMQ equivalent is: `socket.setsockopt_string(zmq.SUBSCRIBE, unicode(''))` – Paul McElroy Jun 30 '17 at 00:01
  • The combintation of C++ and Python works fine for me. I'm using Python3 though and replaced `unicode('')` with `u""`. (Of course, I also added the `except` branch which is missing in the question) – Henri Menke Jun 30 '17 at 00:06
  • In the past, I had some strange mismatches of libzmq 3.x and 4.x for pub/sub sockets. Depending on how you build your non-C language bindings, those could end up using old packages. It is also possible that the real issue at the time was that the PyZMQ Python side was actually built for 4.x, but it ended up dynamically linking to a 3.x version of libzmq.so, or vice versa. This even meant that the version function returned the "right" version, but it was still binding to the wrong shared object. – cnettel Jun 30 '17 at 00:36

2 Answers2

0

Try this snippet code as subscribing with :

import zmq
import time

port = "5555"

context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.setsockopt(zmq.SUBSCRIBE, '')
socket.setsockopt(zmq.CONFLATE, 1)  # last msg only.
socket.connect("tcp://localhost:%s" % port)  # must be placed after above options.

while 1:
    time.sleep(1)
    data = socket.recv()
    print data

[NOTE]:

If not worked, remove the following line in your C++ ZeroMQ publisher:

socket.setsockopt(ZMQ_CONFLATE, 1);

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
0

You are sending a C string (without the null termination).

You are trying to receive a python or java string.

They are probably not compatible and the recvStr / recv_string and are probably blocking waiting for details of where the string ends.

Just use the standard recv functions (not the string versions).

James Harvey
  • 912
  • 4
  • 6