1

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?

user3666197
  • 1
  • 6
  • 50
  • 92
Yves
  • 11,597
  • 17
  • 83
  • 180
  • 1
    Yes, when sockets are REQ-REP then REP must send something back. From what you described, it seems that a pipeline pattern is what you'd benefit from (PUSH - PULL sockets). They are unidirectional, when a PUSH sends a message then PULL just has to read it. You can use combination of two PUSH-PULL pairs to implement async REQ-REP. You can also use the [ROUTER-DEALER](http://zguide.zeromq.org/php:chapter3#The-DEALER-to-ROUTER-Combination) combination which is what I described (async req-rep). – Mjh Sep 25 '15 at 14:29
  • @Mjh thanks a lot. – Yves Sep 25 '15 at 14:33
  • No problem, have fun with ZeroMQ! :) – Mjh Sep 25 '15 at 14:35

1 Answers1

1

ZeroMQ is a very smart and very powerful LEGO-style toolbox

Still, it cannot provide things the very way you ask it. If there was some reason to develop a highly scaleable formal communication pattern, that mimicks REQ-asks/REP-replies, then there is this principal reason behind ZeroMQ architecture, why your attempts to get other than this expected behaviour simply have to fail.

Yes, REQ-asks and principally waits, till REP-reply is REQ-read-in

For formally non-prescribed peer-to-peer behaviour, one may implement PAIR/PAIR Formal Communication Pattern and enjoy the full freedom.

The best next step?

What I can do for your further questions is to direct you to see a bigger picture on this subject with more arguments, a simple signalling-plane / messaging-plane illustration and a direct link to a must-read book from Pieter HINTJENS.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92