ZMQ_CONNECT(3)

✨ ZMQ_CONNECT(3) ✨

okay bestie, welcome to the 0MQ Manual entry for zmq_connect πŸ’– this is the one where you actually reach out to the world. let's get into it 🫢


🌸 NAME 🌸

zmq_connect β€” create outgoing connection from socket


⭐ SYNOPSIS ⭐

int zmq_connect (void *socket, const char *endpoint);

πŸ’… DESCRIPTION πŸ’…

so zmq_connect() does the thing where it hooks your socket up to an endpoint and then accepts incoming connections on that endpoint. it's giving "i'm making the first move" energy πŸ”₯

the endpoint is a string that looks like transport:// followed by an address. the transport bit says which underlying protocol you're using, and the address bit is the transport-specific address you're connecting to. simple as that πŸ’

0MQ ships the following transports, and ngl, they each have their own whole vibe:

  • tcp β€” unicast transport using TCP, see zmq_tcp(7) 🌐
  • ipc β€” local inter-process communication transport, see zmq_ipc(7) 🏠
  • inproc β€” local in-process (inter-thread) communication transport, see zmq_inproc(7) 🧡
  • pgm, epgm β€” reliable multicast transport using PGM, see zmq_pgm(7) πŸ“‘
  • vmci β€” virtual machine communications interface (VMCI), see zmq_vmci(7) πŸ–₯️
  • udp β€” unreliable unicast and multicast using UDP, see zmq_udp(7) 🎈

every single 0MQ socket type except ZMQ_PAIR and ZMQ_CHANNEL supports one-to-many and many-to-one semantics. the precise semantics depend on the socket type and are defined over in zmq_socket(3). iykyk πŸ’œ

Note πŸ“Œ β€” for most transports and socket types the connection is not performed immediately, but as needed by 0MQ. so a successful call to zmq_connect() does not mean the connection was, or even 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 doesn't matter at all. the ZMQ_PAIR and ZMQ_CHANNEL sockets are the exception tho, because they do not automatically reconnect to endpoints πŸ™ˆ
Note πŸ“Œ β€” following a zmq_connect(), for socket types except for ZMQ_ROUTER, the socket enters its normal ready state. by contrast, following a zmq_bind() alone, the socket enters a mute state in which the socket blocks or drops messages according to the socket type, as defined in zmq_socket(3). a ZMQ_ROUTER socket enters its normal ready state for a specific peer only when handshaking is complete for that peer, and that can take an arbitrary amount of time. main character handshake fr 🀝
Note πŸ“Œ β€” for some socket types, multiple connections to the same endpoint don't really make sense (see https://github.com/zeromq/libzmq/issues/788). for those socket types, any attempt to connect to an already connected endpoint is silently ignored (i.e., returns zero). this behavior applies to ZMQ_DEALER, ZMQ_SUB, ZMQ_PUB, and ZMQ_REQ socket types. no drama, no error, just vibes πŸ’€

πŸŽ€ RETURN VALUE πŸŽ€

zmq_connect() returns zero if it slays. otherwise it returns -1 and sets errno to one of the values defined below 😭


πŸ”₯ ERRORS πŸ”₯

  • EINVAL β€” the endpoint supplied is invalid.
  • EPROTONOSUPPORT β€” the requested transport protocol is not supported.
  • ENOCOMPATPROTO β€” the requested transport protocol is not compatible with the socket type.
  • ETERM β€” the 0MQ context associated with the specified socket was terminated.
  • ENOTSOCK β€” the provided socket was invalid.
  • EMTHREAD β€” no I/O thread is available to accomplish the task.

🌈 EXAMPLE 🌈

connecting a subscriber socket to an in-process and a TCP transport. iconic behavior honestly ✨

/* Create a ZMQ_SUB socket */
void *socket = zmq_socket (context, ZMQ_SUB);
assert (socket);
/* Connect it to an in-process transport with the address 'my_publisher' */
int rc = zmq_connect (socket, "inproc://my_publisher");
assert (rc == 0);
/* Connect it to the host server001, port 5555 using a TCP transport */
rc = zmq_connect (socket, "tcp://server001:5555");
assert (rc == 0);

πŸ‘‘ SEE ALSO πŸ‘‘

zmq_bind(3) zmq_socket(3) zmq(7)


πŸ’– AUTHORS πŸ’–

this page was written by the 0MQ community 🫢 to make a change please read the 0MQ Contribution Policy at http://www.zeromq.org/docs:contributing.


and that's the whole page bestie, you ate πŸ’… this was 0MQ 4.3.5, dated 10/09/2023 β€” go forth and connect responsibly πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from