Created
February 23, 2012 23:06
-
-
Save cmaureir/1895599 to your computer and use it in GitHub Desktop.
ZeroMQ empty message issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm trying to develop the following messaging structure | |
A -> B -> C | |
A: | |
-send a message to B (send) | |
-waits for a B answer (recv) | |
B: | |
-receive the A message (recv) | |
-send a message to C (send) | |
-waits for a C answer (recv) | |
-send an answer to A (send) | |
C: | |
-receive the B message (recv) | |
-send an answer to B (send) | |
Printing the messages sizes, I note that when B receive a message prints the properly message_size but when C receive the message, the message_size is 0, which is incorrect. | |
This is a minimal version of the interaction, the real code works with largest data. | |
I attached the code of the 3 files, and the execution sequence in different linux terminals is: | |
- Start C | |
- Start B | |
- Start A | |
----input.cpp (A) ----- | |
#include <zmq.hpp> | |
#include <iostream> | |
#include <iostream> | |
using namespace std; | |
int main(int argc, char *argv[]) { | |
zmq::context_t context (1); | |
zmq::socket_t socket (context, ZMQ_REQ); | |
socket.connect ("tcp://127.0.0.1:5000"); | |
int msg_num = 0; | |
while(1){ | |
zmq::message_t request (5); | |
memcpy ((void *) request.data(), "hello", 5); | |
cout << "Message to controller " << msg_num << " sent" << endl; | |
socket.send (request); | |
zmq::message_t reply; | |
socket.recv (&reply); | |
cout << "Message from controller " << msg_num << " received." << endl; | |
msg_num++; | |
} | |
return 0; | |
} | |
----- controller.cpp (B) ----- | |
#include "zmq.hpp" | |
#include <iostream> | |
using namespace std; | |
int main(int argc, char *argv[]) { | |
int msg_num = 0; | |
zmq::context_t context (1); | |
zmq::socket_t receiver(context, ZMQ_REP); | |
zmq::socket_t sender(context, ZMQ_REQ); | |
receiver.bind("tcp://127.0.0.1:5000"); | |
sender.connect("tcp://127.0.0.101:5050"); | |
zmq::message_t input_request; | |
zmq::message_t input_reply; | |
zmq::message_t output_request; | |
zmq::message_t output_reply; | |
while (1){ | |
receiver.recv(&input_request); | |
cout << "Message from input " << msg_num << " received" << endl; | |
cout << "Input size: " << input_request.size() << endl; // this is correct! | |
sleep (1); | |
memcpy((void*)output_request.data(), "hola", 4); | |
cout << "Message to OutputReceiver " << msg_num << " sent" << endl; | |
sender.send(output_request); | |
sender.recv (&output_reply); | |
cout << "Message from OutputReceiver " << msg_num << " received" << endl; | |
zmq::message_t input_reply; | |
memcpy ((void *)input_reply.data(), "ok", 2); | |
receiver.send (input_reply); | |
} | |
return 0; | |
} | |
----- output.cpp (C) ----- | |
#include <zmq.hpp> | |
#include <iostream> | |
using namespace std; | |
int main(int argc, char *argv[]) { | |
zmq::context_t context(1); | |
zmq::socket_t receiver(context, ZMQ_REP); | |
receiver.bind("tcp://127.0.0.101:5050"); | |
zmq::message_t output_request; | |
zmq::message_t reply; | |
while (1){ | |
receiver.recv(&output_request); | |
cout << "Message from controller received" << endl; | |
cout << "Output size: " << output_request.size() << endl; // INCORRECT! | |
sleep(1); | |
memcpy ((void *) reply.data (), "ok", 2); | |
receiver.send (reply); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed,
it is necessary to "rebuild" the message in each iteration if we don't create a new message_t inside the loop.
The following solutions works properly.
----- sol 1-----
while (1){
zmq::message_t msg;
...
socket.send(msg);
}
----- sol 2 ----
zmq::message_t msg;
while (1){
msg.rebuild(SIZE);
...
socket.send(msg);
}