[nanomsg] Trouble with "Hello World" REQ/REP Example

  • From: Mike Craig <mkscrg@xxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Mon, 20 May 2013 16:49:52 -0400

Hi all,

I've tried to port the first example from the ZMQ guide to nanomsg, but I'm
running into some trouble. The example I'm referring to is the "Hello
World" server/client shown here:

http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive

My nanomsg code is a direct port, I think:

server.c:

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>

int main(void) {
    int s = nn_socket(AF_SP, NN_REP);
    int rc = nn_bind(s, "tcp://*:5555");
    assert (rc >= 0);

    while (1) {
        char buffer[10];
        nn_recv(s, buffer, 10, 0);
        printf("Received Hello\n");
        nn_send(s, "World", 5, 0);
        sleep(1);
    }

    return 0;
}

client.c:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>

int main (void) {
    printf("Connecting to hello world server…\n");
    int s = nn_socket(AF_SP, NN_REQ);
    nn_connect(s, "tcp://localhost:5555");

    int request_nbr;
    for (request_nbr = 0; request_nbr != 10; request_nbr++) {
        char buffer [10];
        printf("Sending Hello %d...\n", request_nbr);
        nn_send(s, "Hello", 5, 0);
        nn_recv(s, buffer, 10, 0);
        printf("Received World %d\n", request_nbr);
    }
    nn_close(s);
    return 0;
}

But when run, the server seems to choke after a single client has finished.
Successive clients block after printing "Sending Hello 0...". Specifically,
output looks like this:

server:

$ ./server
Received Hello
< 8 reps omitted >
Received Hello

client:

$ ./client
Connecting to hello world server…
Sending Hello 0...
Received World 0
< 8 reps omitted >
Sending Hello 9...
Received World 9
$ ./client
Connecting to hello world server…
Sending Hello 0...

Am I missing something, or is this a bug?

Cheers,
Mike

Other related posts: