ZMQ\_CONNECT\_PEER(3) 0MQ Manual ZMQ\_CONNECT\_PEER(3)

# [NAME](#NAME)

zmq\_connect\_peer - create outgoing connection from socket and return the connection routing id in thread-safe and atomic way.

# [SYNOPSIS](#SYNOPSIS)

**uint32\_t zmq\_connect\_peer (void \*socket, const char \*endpoint);**

# [DESCRIPTION](#DESCRIPTION)

The *zmq\_connect\_peer()* function connects a *ZMQ\_PEER* socket to an *endpoint* and then returns the endpoint *routing\_id*.

The *endpoint* is a string consisting of a *transport*:// followed by an *address*. The *transport* specifies the underlying protocol to use. The *address* specifies the transport-specific address to connect to.

The function is supported only on the *ZMQ\_PEER* socket type and would return 0 with *errno* set to *ENOTSUP* otherwise.

The *zmq\_connect\_peer()* support the following transports:

*tcp*

unicast transport using TCP, see [**zmq\_tcp**(7)](/man/os/zeromq/zmq_tcp.7)

*ipc*

local inter-process communication transport, see [**zmq\_ipc**(7)](/man/os/zeromq/zmq_ipc.7)

*inproc*

local in-process (inter-thread) communication transport, see [**zmq\_inproc**(7)](/man/os/zeromq/zmq_inproc.7)

*ws*

unicast transport using WebSockets, see [**zmq\_ws**(7)](/man/os/zeromq/zmq_ws.7)

*wss*

unicast transport using WebSockets over TLS, see [**zmq\_wss**(7)](/man/os/zeromq/zmq_wss.7)

# [RETURN VALUE](#RETURN_VALUE)

The *zmq\_connect\_peer()* function returns the peer *routing\_id* if successful. Otherwise it returns 0 and sets *errno* to one of the values defined below.

# [ERRORS](#ERRORS)

**EINVAL**

The endpoint supplied is invalid.

**EPROTONOSUPPORT**

The requested *transport* protocol is not supported with *ZMQ\_PEER*.

**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.

**ENOTSUP**

The socket is not of type *ZMQ\_PEER*.

**EFAULT**

The *ZMQ\_IMMEDIATE* option is set on the socket.

# [EXAMPLE](#EXAMPLE)

**Connecting a peer socket to a TCP transport and sending a message**.

```
/* Create a ZMQ_SUB socket */
void *socket = zmq_socket (context, ZMQ_PEER);
assert (socket);
/* Connect it to the host server001, port 5555 using a TCP transport */
uint32_t routing_id = zmq_connect (socket, "tcp://server001:5555");
assert (routing_id == 0);
/* Sending a message to the peer  */
zmq_msg_t msg;
int rc = zmq_msg_init_data (&msg, "HELLO", 5, NULL, NULL);
assert (rc == 0);
rc = zmq_msg_set_routing_id (&msg, routing_id);
assert (rc == 0);
rc = zmq_msg_send (&msg, socket, 0);
assert (rc == 5);
rc = zmq_msg_close (&msg);
assert (rc == 0);
```

# [SEE ALSO](#SEE_ALSO)

[**zmq\_connect**(3)](/man/os/zeromq/zmq_connect.3) [**zmq\_bind**(3)](/man/os/zeromq/zmq_bind.3) [**zmq\_socket**(3)](/man/os/zeromq/zmq_socket.3) [**zmq**(7)](/man/os/zeromq/zmq.7)

# [AUTHORS](#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**.

08/29/2026 0MQ 4.3.5

---

Document: zmq_connect_peer(3) from zeromq 4.3.5-r13 (Chainguard OS)
Source: https://github.com/zeromq/libzmq
