I'm using the pattern REQ
/REP
of ZeroMQ.
Normally this pattern works like this:
REP.bind(localhost with a port)
REQ.connect(localhost with the same port)
REQ sends a REQ-message to REP
REP sends a REP-message to REQ back
Now, I want to remove the last step.
Meaning that REP
doesn't send back anything, it just receives the messages from REQ
.
I know there another pattern named PUB
/SUB
to do this but I just want to use REQ
/REP
.
Here is my code:
// REQ
int main()
{
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5555");
const char * msgSent = "I'm agent";
zmq_send(requester, msgSent, strlen(msgSent), 0);
zmq_close(requester);
zmq_ctx_destroy(context);
system("pause");
return 0;
}
// REP
int main()
{
void *context = zmq_ctx_new();
void *responder = zmq_socket(context, ZMQ_REP);
zmq_bind(responder, "tcp://*:5555");
while (true) {
char buffer[50] = {'\0'};
cout << "receiving..." << endl;
zmq_recv(responder, buffer, 50, 0);
cout << buffer << endl << "end";
}
system("pause");
return 0;
}
I first run REP
, it will show "receiving..." on the console, meaning that it is waiting for some message.
The I run REQ
, which sends a message to REQ
.
After that, the console of REP
will show "I'm agent" BUT then the while
will keep going. It seems that zmq_recv
is NON-BLOCKING.
I thought it would stop after receiving "I'm agent" but it didn't. But If I make REP to send something back to REQ
, everything will be fine.
WHY?
MUST REP
send something back?