I have just started studying the ZeroMQ messaging library from Chapter 1 of the guide (available here). While I understand the well-written examples, I don't get one small point of the syntax: specifically the way the TCP sockets are connected/binded to differently in the client and server for the hello world example. This code is also supposed to be a demo of the request response pattern. (You can see the full code pasted below. )
The server binds to the TCP socket as int rc = zmq_bind (responder, "tcp://*:5555");
whereas the client connects to the TCP socket as zmq_connect (requester, "tcp://localhost:5555");
Why don't both use "tcp://*:5555"
( or "tcp://localhost:5555"
for that matter) in the function call while connecting and binding?
Code for hwserver.c
// Hello World server#include <zmq.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <assert.h>int main (void){ // Socket to talk to clients void *context = zmq_ctx_new (); void *responder = zmq_socket (context, ZMQ_REP); int rc = zmq_bind (responder, "tcp://*:5555"); assert (rc == 0); while (1) { char buffer [10]; zmq_recv (responder, buffer, 10, 0); printf ("Received Hello\n"); sleep (1); // Do some 'work' zmq_send (responder, "World", 5, 0); } return 0;}
Code for hwclient.c
// Hello World client#include <zmq.h>#include <string.h>#include <stdio.h>#include <unistd.h>int main (void){ printf ("Connecting to hello world server...\n"); void *context = zmq_ctx_new (); void *requester = zmq_socket (context, ZMQ_REQ); zmq_connect (requester, "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); zmq_send (requester, "Hello", 5, 0); zmq_recv (requester, buffer, 10, 0); printf ("Received World %d\n", request_nbr); } zmq_close (requester); zmq_ctx_destroy (context); return 0;}