---
name: "zmq_cpp"
section: "7"
platform: "linux"
package: "libzmq-dev"
version: "2.2.0+dfsg-6"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/zmq_cpp"
---

# zmq\_cpp(7)

interface between 0MQ and C++ applications

## NAME

zmq\_cpp - interface between 0MQ and C++ applications

## SYNOPSIS

**#include \<zmq.hpp\>**

**c++** \[*flags*\] *files* **-lzmq** \[*libraries*\]

## DESCRIPTION

This manual page describes how the 0MQ C++ language binding maps to the underlying 0MQ C library functions.

All 0MQ constants defined by *zmq.h* are also available to the C++ language binding.

The following classes are provided in the *zmq* namespace:

### Context

The *context\_t* class encapsulates functionality dealing with the initialisation and termination of a 0MQ *context*.

**Constructor**

```
context_t::context_t(int io_threads)
```

Maps to the *zmq\_init()* function, as described in [**zmq\_init**(3)](/zmq_init/3.md).

**Destructor**

```
context_t::~context_t(void)
```

Maps to the *zmq\_term()* function, as described in [**zmq\_term**(3)](/zmq_term/3.md).

**Methods**

None.

### Socket

The *socket\_t* class encapsulates a 0MQ socket.

**Constructor**

```
socket_t::socket_t(context_t &context, int type)
```

Maps to the *zmq\_socket()* function, as described in [**zmq\_socket**(3)](/zmq_socket/3.md).

**Destructor**

```
socket_t::~socket_t(void)
```

Calls the *zmq\_close()* function, as described in [**zmq\_close**(3)](/zmq_close/3.md).

**Methods**

```
void socket_t::getsockopt(int option_name, void *option_value, size_t
*option_len)
```

Maps to the *zmq\_getsockopt()* function, as described in [**zmq\_getsockopt**(3)](/zmq_getsockopt/3.md).

```
void socket_t::setsockopt(int option_name, const void *option_value, size_t
option_len)
```

Maps to the *zmq\_setsockopt()* function, as described in [**zmq\_setsockopt**(3)](/zmq_setsockopt/3.md).

```
void socket_t::bind(const char *endpoint)
```

Maps to the *zmq\_bind()* function, as described in [**zmq\_bind**(3)](/zmq_bind/3.md).

```
void socket_t::connect(const char *endpoint)
```

Maps to the *zmq\_connect()* function, as described in [**zmq\_connect**(3)](/zmq_connect/3.md).

```
bool socket_t::send(message_t &msg, int flags = 0)
```

Maps to the *zmq\_send()* function, as described in [**zmq\_send**(3)](/zmq_send/3.md). Returns true if message is successfully sent, false if it is not.

```
bool socket_t::recv(message_t *msg, int flags = 0)
```

Maps to the *zmq\_recv()* function, as described in [**zmq\_recv**(3)](/zmq_recv/3.md). Returns true if message is successfully received, false if it is not.

### Message

The *zmq::message\_t* class encapsulates the *zmq\_msg\_t* structure and functions to construct, destruct and manipulate 0MQ messages.

**Constructor**

```
message_t::message_t(void)
message_t::message_t(size_t size)
message_t::message_t(void *data, size_t size, free_fn *ffn)
```

These map to the *zmq\_msg\_init()*, *zmq\_msg\_init\_size()* and *zmq\_msg\_init\_data()* functions, described in [**zmq\_msg\_init**(3)](/zmq_msg_init/3.md), [**zmq\_msg\_init\_size**(3)](/zmq_msg_init_size/3.md) and [**zmq\_msg\_init\_data**(3)](/zmq_msg_init_data/3.md) respectively.

**Destructor**

```
message_t::~message_t(void)
```

Calls the *zmq\_msg\_close()* function, as described in [**zmq\_msg\_close**(3)](/zmq_msg_close/3.md).

**Methods**

```
void *message_t::data (void)
```

Maps to the *zmq\_msg\_data()* function, as described in [**zmq\_msg\_data**(3)](/zmq_msg_data/3.md).

```
size_t message_t::size (void)
```

Maps to the *zmq\_msg\_size()* function, as described in [**zmq\_msg\_size**(3)](/zmq_msg_size/3.md).

```
void message_t::copy (message_t *src)
```

Maps to the *zmq\_msg\_copy()* function, as described in [**zmq\_msg\_copy**(3)](/zmq_msg_copy/3.md).

```
void message_t::move (message_t *src)
```

Maps to the *zmq\_msg\_move()* function, as described in [**zmq\_msg\_move**(3)](/zmq_msg_move/3.md).

```
message_t::rebuild(void)
message_t::rebuild(size_t size)
message_t::rebuild(void *data, size_t size, free_fn *ffn)
```

Equivalent to calling the *zmq\_msg\_close()* function followed by the corresponding *zmq\_msg\_init()* function.

### Input/output multiplexing

```
int poll (zmq_pollitem_t *items, int nitems, long timeout = -1)
```

The *poll()* function is a namespaced equivalent of the *zmq\_poll()* function, as described in [**zmq\_poll**(3)](/zmq_poll/3.md).

**Note**\

To obtain a 0MQ *socket* for use in a *zmq\_pollitem\_t* structure, you should cast an instance of the *socket\_t* class to (void \*).

## ERROR HANDLING

All errors reported by the underlying 0MQ C library functions are automatically converted to exceptions by the C++ language binding. The *zmq::error\_t* class is derived from the *std::exception* class and uses the *zmq\_strerror()* function to convert the error code to human-readable string.

## EXAMPLE

```
zmq::context_t ctx (1);
zmq::socket_t s (ctx, ZMQ_PUB);
s.connect ("tcp://192.168.0.115:5555");
zmq::message_t msg (100);
memset (msg.data (), 0, 100);
s.send (msg);
```

## SEE ALSO

[**zmq**(7)](/zmq/7.md)

## AUTHORS

This manual page was written by the 0MQ community.
