Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ZeroMQ req-rep give me an empty response between C++ and Python?

Tags:

c++

python

zeromq

I'm trying to bridge between a Python client and a C++ based server using ZeroMQ but I'm struggling to get the correct response returned from the server back to the client.

The Python client looks like this:

import zmq
import time

# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.REQ)

socket.connect ("tcp://127.0.0.1:5563")

requestId = 0
while True:
    request = "Message %d"%requestId
    print ("Sending '%s'.. "%request)
    socket.send_string(request)

    response = socket.recv()
    print ("Response:",response)

    requestId += 1
    time.sleep(1)

The C++ server looks like this:

zmq::context_t* context = new zmq::context_t();
zmq::socket_t* publisher = new zmq::socket_t(*context, ZMQ_REP);

unsigned int port = 5563;
std::ostringstream bindDest;
bindDest << "tcp://*:" << port;
publisher->bind(bindDest.str());

zmq::message_t request(0);

bool notInterrupted = true;
while (notInterrupted)
{
    // Wait for a new request to act upon.
    publisher->recv(&request, 0);

    // Turn the incoming message into a string
    char* requestDataBuffer = static_cast<char*>(request.data());
    std::string requestStr(requestDataBuffer, request.size());

    printf("Got request: %s\n", requestStr.c_str());

    // Call the delegate to get a response we can pass back.
    std::string responseString = requestStr + " response";
    printf("Responding with: %s\n", responseString.c_str());

    // Turn the response string into a message.
    zmq::message_t responseMsg(responseString.size());
    char* responseDataBuffer = static_cast<char*>(responseMsg.data());
    const int OffsetToCopyFrom = 0;
    responseString.copy(responseDataBuffer, responseString.size(), OffsetToCopyFrom);

    // Pass back our response.
    publisher->send(&responseMsg, 0);
}

When I run this, the client reports:

Sending 'Message 0'..

The server reports:

Got request: Message 0

Responding with: Message 0 response

But the python receives a blank message:

Response: b''

If I replace the C++ version, with a Python implementation as follows it works as expected:

import zmq

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

socket.bind ("tcp://127.0.0.1:5563")

replyId = 0
while True:
    request = socket.recv_string()
    print("Received: "+request)

    response = request+" response %d"%replyId
    print ("Sending response: "+response)
    socket.send_string(response)
    replyId += 1

What am I doing wrong in the C++ version?

Update - Checked versions...

Reading around other people's problems, one thing people have suggested is that different versions sometimes cause issues. I double-checked and Python is using v4.1.6 whereas C++ is on v4.0.4.

I'm using the C++ pre-built libraries from here: http://zeromq.org/distro:microsoft-windows so I guess that could be the cause? I made a similar setup to publish from C++ to Python and that worked fine but perhaps something in the Req-Rep area has changed.

Update2 - It doesn't appear to be the versions...

After a lot of hacking around I finally managed to get v4.1.6 to build for the C++ version of the server and I still get the same empty message.

like image 781
Jon Cage Avatar asked Feb 28 '26 02:02

Jon Cage


1 Answers

You need to send the buffer, not the pointer.

publisher->send(responseMsg, 0);

did the trick when I tested your code.

Hope this helps, Hannu

like image 78
Hannu Avatar answered Mar 01 '26 16:03

Hannu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!