Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ZeroMQ connect return 0 even when no server listening?

Tags:

c

zeromq

I am using ZeroMQ 3.2 in C. The problem I have is whenever I try to connect to some remote endpoint

void* context = zmq_ctx_new();
void* socket = zmq_socket(context, ZMQ_DEALER);
int rc = zmq_connect(socket, addr);

rc is ALWAYS 0, meaning every thing is O.K. as mentioned by the API. Even if there is no server listening at all.

Do I miss something?

like image 366
AlexLiesenfeld Avatar asked Feb 04 '13 13:02

AlexLiesenfeld


2 Answers

Everything is as it should. An advantage (depending on how you look at it of course) of ZeroMq is that you do not have to connect after the server was started, also meaning that it is ok to connect even if the connection to the server is temporarily down. ZeroMq will keep the connection (request) and try again.

The Api docs for zmq_connect() states that:

For most transports and socket types the connection is not performed immediately but as needed by ØMQ. Thus a successful call to zmq_connect() does not mean that the connection was or could actually be established. Because of this, for most transports and socket types the order in which a server socket is bound and a client socket is connected to it does not matter. The first exception is when using the inproc:// transport: you must call zmq_bind() before calling zmq_connect(). The second exception are ZMQ_PAIR sockets, which do not automatically reconnect to endpoints.

As seen in the docs, there is no error code for non-responding/existing endpoint. Basically, as long as you provide a valid endpoint (and you do not for example hit the socket-per-process limit for example), you should be good.

like image 114
Jakob Möllås Avatar answered Oct 05 '22 21:10

Jakob Möllås


As the guide states:

As soon as the client node does zmq_connect() the connection exists and that node can start to write messages to the socket. At some stage (hopefully before messages queue up so much that they start to get discarded, or the client blocks), the server comes alive, does a zmq_bind() and ØMQ starts to deliver messages.

like image 22
AlexLiesenfeld Avatar answered Oct 05 '22 21:10

AlexLiesenfeld