### Install HTML Documentation
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Installs HTML documentation files to the doc/zmq directory if the WITH_DOC option is enabled and not building a framework. This is for reference guides.
```cmake
if(WITH_DOC)
if(NOT ZMQ_BUILD_FRAMEWORK)
install(
FILES ${html-docs}
DESTINATION doc/zmq
COMPONENT RefGuide)
endif()
endif()
```
--------------------------------
### ZeroMQ Socket Monitor Setup Example
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_socket_monitor_versioned.adoc
Demonstrates how to set up socket monitoring using `zmq_socket_monitor_versioned`. It shows how to create sockets, attempt to monitor with an unsupported protocol, and then successfully monitor events on client and server sockets using in-process endpoints.
```c
int main (void)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
// We'll monitor these two sockets
void *client = zmq_socket (ctx, ZMQ_DEALER);
assert (client);
void *server = zmq_socket (ctx, ZMQ_DEALER);
assert (server);
// Socket monitoring only works over inproc://
int rc = zmq_socket_monitor_versioned (client, "tcp://127.0.0.1:9999", 0, 2);
assert (rc == -1);
assert (zmq_errno () == EPROTONOSUPPORT);
// Monitor all events on client and server sockets
rc = zmq_socket_monitor_versioned (client, "inproc://monitor-client", ZMQ_EVENT_ALL, 2);
assert (rc == 0);
rc = zmq_socket_monitor_versioned (server, "inproc://monitor-server", ZMQ_EVENT_ALL, 2);
assert (rc == 0);
// Create two sockets for collecting monitor events
void *client_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (client_mon);
void *server_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (server_mon);
// Connect these to the inproc endpoints so they'll get events
rc = zmq_connect (client_mon, "inproc://monitor-client");
assert (rc == 0);
rc = zmq_connect (server_mon, "inproc://monitor-server");
assert (rc == 0);
// Now do a basic ping test
rc = zmq_bind (server, "tcp://127.0.0.1:9998");
assert (rc == 0);
rc = zmq_connect (client, "tcp://127.0.0.1:9998");
assert (rc == 0);
bounce (client, server);
// Close client and server
close_zero_linger (client);
close_zero_linger (server);
// Now collect and check events from both sockets
int event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_CONNECT_DELAYED)
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED);
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_MONITOR_STOPPED);
// This is the flow of server events
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_LISTENING);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_ACCEPTED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CLOSED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_MONITOR_STOPPED);
// Close down the sockets
close_zero_linger (client_mon);
close_zero_linger (server_mon);
zmq_ctx_term (ctx);
return 0 ;
}
```
--------------------------------
### Create a shared queue proxy
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_proxy.adoc
This example demonstrates setting up frontend and backend sockets, binding them to TCP ports, and then starting the proxy to function as a shared queue. The proxy will run until the ZeroMQ context is terminated.
```c
// Create frontend and backend sockets
void *frontend = zmq_socket (context, ZMQ_ROUTER);
assert (frontend);
void *backend = zmq_socket (context, ZMQ_DEALER);
assert (backend);
// Bind both sockets to TCP ports
assert (zmq_bind (frontend, "tcp://*:5555") == 0);
assert (zmq_bind (backend, "tcp://*:5556") == 0);
// Start the queue proxy, which runs until ETERM
zmq_proxy (frontend, backend, NULL);
```
--------------------------------
### Configure and Install Documentation
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Configures documentation files and installs them to the share/zmq directory if not building a framework. This ensures documentation is available with the installed package.
```cmake
foreach(readme ${readme-docs})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${readme} ${CMAKE_CURRENT_BINARY_DIR}/${readme}.txt)
if(NOT ZMQ_BUILD_FRAMEWORK)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${readme}.txt DESTINATION share/zmq)
endif()
endforeach()
```
--------------------------------
### Install libzmq using vcpkg
Source: https://github.com/zeromq/libzmq/blob/master/README.md
Installs libzmq using the vcpkg cross-platform package manager. This involves cloning the vcpkg repository, bootstrapping it, and then installing the zeromq package.
```bash
git clone https://github.com/microsoft/vcpkg.git
./bootstrap-vcpkg.bat # For powershell
./bootstrap-vcpkg.sh # For bash
./vcpkg install zeromq
```
--------------------------------
### zmq_setsockopt Examples
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_setsockopt.adoc
Examples demonstrating the usage of zmq_setsockopt for subscribing to messages and setting I/O thread affinity.
```APIDOC
== EXAMPLE
.Subscribing to messages on a 'ZMQ_SUB' socket
----
/* Subscribe to all messages */
rc = zmq_setsockopt (socket, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
/* Subscribe to messages prefixed with "ANIMALS.CATS" */
rc = zmq_setsockopt (socket, ZMQ_SUBSCRIBE, "ANIMALS.CATS", 12);
----
.Setting I/O thread affinity
----
int64_t affinity;
/* Incoming connections on TCP port 5555 shall be handled by I/O thread 1 */
affinity = 1;
rc = zmq_setsockopt (socket, ZMQ_AFFINITY, &affinity, sizeof (affinity));
assert (rc);
rc = zmq_bind (socket, "tcp://lo:5555");
assert (rc);
/* Incoming connections on TCP port 5556 shall be handled by I/O thread 2 */
affinity = 2;
rc = zmq_setsockopt (socket, ZMQ_AFFINITY, &affinity, sizeof (affinity));
assert (rc);
rc = zmq_bind (socket, "tcp://lo:5556");
assert (rc);
----
```
--------------------------------
### Install Package Configuration Files
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Installs the exported targets, CMake configuration file, and version file to the specified installation directory. This makes the package discoverable by other CMake projects.
```cmake
if(BUILD_SHARED OR BUILD_STATIC)
install(
EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${ZEROMQ_CMAKECONFIG_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${ZEROMQ_CMAKECONFIG_INSTALL_DIR})
endif()
```
--------------------------------
### Install Performance Tools
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Installs the compiled performance tools to the bin directory. This command is executed if ZMQ_BUILD_FRAMEWORK is not enabled.
```cmake
install(TARGETS ${perf-tool} RUNTIME DESTINATION bin COMPONENT PerfTools)
```
--------------------------------
### Install libzmq on OSX using Homebrew
Source: https://github.com/zeromq/libzmq/blob/master/README.md
Installs libzmq on macOS using the Homebrew package manager.
```bash
brew install zeromq
```
--------------------------------
### Configure NSIS Installer Icons and Compression
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Sets the installer icon and defines the compression method for the NSIS installer. Ensure the icon path has at least four backslashes.
```cmake
set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\installer.ico")
set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}\\installer.ico")
set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\branding.bmp")
set(CPACK_NSIS_COMPRESSOR "/SOLID lzma")
```
--------------------------------
### Setting and getting ZMQ_THREAD_NAME_PREFIX
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_ctx_get_ext.adoc
This example demonstrates how to set a prefix for internal ZeroMQ thread names using zmq_ctx_set() and then retrieve it using zmq_ctx_get(). Ensure the buffer is large enough to hold the prefix.
```c
void *context = zmq_ctx_new ();
const char prefix[] = "MyApp";
size_t prefixLen = sizeof(prefix);
zmq_ctx_set (context, ZMQ_THREAD_NAME_PREFIX, &prefix, &prefixLen);
char buff[256];
size_t buffLen = sizeof(buff);
int rc = zmq_ctx_get (context, ZMQ_THREAD_NAME_PREFIX, &buff, &buffLen);
assert (rc == 0);
assert (buffLen == prefixLen);
```
--------------------------------
### Install libzmq on Debian 9 using OBS
Source: https://github.com/zeromq/libzmq/blob/master/README.md
Installs the latest stable release of libzmq3-dev on Debian 9 using the Open Build Service. Ensure you are not using DRAFT APIs if stability is required.
```bash
echo "deb http://download.opensuse.org/repositories/network:/messaging:/zeromq:/release-stable/Debian_9.0/ ./">> /etc/apt/sources.list
wget https://download.opensuse.org/repositories/network:/messaging:/zeromq:/release-stable/Debian_9.0/Release.key -O- | sudo apt-key add
apt-get install libzmq3-dev
```
--------------------------------
### Unity Test Setup and Teardown Functions
Source: https://github.com/zeromq/libzmq/blob/master/tests/README.md
Use these setUp and tearDown functions with the unity test framework to manage test contexts and ensure proper socket cleanup. Sockets not closed before tearDown will be forcibly closed with linger=0.
```c
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
```
--------------------------------
### Install Targets for MSVC Builds
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Installs targets, libraries, and headers for MSVC builds, including PDB files for debugging. Components are specified for SDK and Runtime.
```cmake
install(
TARGETS ${target_outputs}
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT SDK)
if(MSVC_IDE)
install(
FILES ${PDB_OUTPUT_DIRECTORY}/\${CMAKE_INSTALL_CONFIG_NAME}/${PDB_NAME}.pdb
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT SDK
OPTIONAL)
else()
install(
FILES ${PDB_OUTPUT_DIRECTORY}/${PDB_NAME}.pdb
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT SDK
OPTIONAL)
endif()
if(BUILD_SHARED)
install(
TARGETS libzmq
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT Runtime)
endif()
```
--------------------------------
### Install Targets for Non-MSVC Builds
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Installs targets, libraries, and headers for non-MSVC builds, including runtime, archive, library, and framework destinations. Public headers are also installed.
```cmake
install(
TARGETS ${target_outputs}
EXPORT ${PROJECT_NAME}-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
FRAMEWORK DESTINATION "Library/Frameworks"
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
```
--------------------------------
### Platform-Specific Definitions
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Adds preprocessor definitions for specific platforms. This example adds -DUNITY_EXCLUDE_MATH_H for QNX systems.
```cmake
add_definitions(-DUNITY_EXCLUDE_MATH_H)
```
--------------------------------
### Sending a multi-part message example
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_send.adoc
This example demonstrates how to send a multi-part message using zmq_send. The ZMQ_SNDMORE flag is used for all parts except the final one.
```APIDOC
## Example: Sending a multi-part message
```c
/* Send a multi-part message consisting of three parts to socket */
rc = zmq_send (socket, "ABC", 3, ZMQ_SNDMORE);
assert (rc == 3);
rc = zmq_send (socket, "DEFGH", 5, ZMQ_SNDMORE);
assert (rc == 5);
/* Final part; no more parts to follow */
rc = zmq_send (socket, "JK", 2, 0);
assert (rc == 2);
```
```
--------------------------------
### Example: Polling Indefinitely
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_poller.adoc
This example demonstrates how to poll indefinitely for input events on both a ZMQ socket and a standard file descriptor using `zmq_poller_add`, `zmq_poller_add_fd`, and `zmq_poller_wait_all`.
```APIDOC
## EXAMPLE
.Polling indefinitely for input events on both a 0MQ socket and a standard socket.
----
void *poller = zmq_poller_new ();
/* First item refers to 0MQ socket 'socket' */
zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN);
/* Second item refers to standard socket 'fd' */
zmq_poller_add_fd (poller, fd, NULL, ZMQ_POLLIN);
zmq_poller_event_t events [2];
/* Poll for events indefinitely */
int rc = zmq_poller_wait_all (poller, events, 2, -1);
assert (rc >= 0);
/* Returned events will be stored in 'events' */
for (int i = 0; i < 2; ++i) {
if (events[i].socket == socket && events[i].events & ZMQ_POLLIN) {
// ...
} else if (events[i].fd == fd && events[i].events & ZMQ_POLLIN)) {
// ...
}
}
zmq_poller_destroy (&poller);
----
```
--------------------------------
### Connecting a Socket with PGM
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_pgm.adoc
Demonstrates how to connect a ZeroMQ socket using the PGM protocol. Examples show connecting to a multicast address using a specific network interface with both Encapsulated PGM and standard PGM.
```c
rc = zmq_connect(socket, "epgm://eth0;239.192.1.1:5555");
assert (rc == 0);
```
```c
rc = zmq_connect(socket, "pgm://192.168.1.1;239.192.1.1:5555");
assert (rc == 0);
```
--------------------------------
### CPack General Settings
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Configures general CPack settings including the generator, package name, description, vendor, contact, and resource files like license. This prepares CPack to build an installer.
```cmake
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
set(CPACK_GENERATOR "NSIS")
set(CPACK_PACKAGE_NAME "ZeroMQ")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "ZeroMQ lightweight messaging kernel")
set(CPACK_PACKAGE_VENDOR "Miru")
set(CPACK_NSIS_CONTACT "Steven McCoy ")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_BINARY_DIR}\\LICENSE.txt")
```
--------------------------------
### Compile individual object file on z/OS
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
Example of compiling a single object file (zmq.o) from the src directory using make on z/OS. Note potential issues with IBM Make.
```bash
cd src
make zmq.o
```
--------------------------------
### Connect and Disconnect a Subscriber Socket
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_disconnect.adoc
This example demonstrates how to create a ZMQ_SUB socket, connect it to a TCP endpoint, and then disconnect from that endpoint. Ensure the context is properly initialized before creating the socket.
```c
/* Create a ZMQ_SUB socket */
void *socket = zmq_socket (context, ZMQ_SUB);
assert (socket);
/* Connect it to the host server001, port 5555 using a TCP transport */
rc = zmq_connect (socket, "tcp://server001:5555");
assert (rc == 0);
/* Disconnect from the previously connected endpoint */
rc = zmq_disconnect (socket, "tcp://server001:5555");
assert (rc == 0);
```
--------------------------------
### Receive a multi-part message
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_msg_recv.adoc
This example shows how to receive a multi-part message by repeatedly calling zmq_msg_recv() and checking the ZMQ_RCVMORE socket option to determine if more parts are available. It initializes, receives, and closes each part.
```c
int more;
size_t more_size = sizeof (more);
do {
/* Create an empty 0MQ message to hold the message part */
zmq_msg_t part;
int rc = zmq_msg_init (&part);
assert (rc == 0);
/* Block until a message is available to be received from socket */
rc = zmq_msg_recv (&part, socket, 0);
assert (rc != -1);
/* Determine if more message parts are to follow */
rc = zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
assert (rc == 0);
zmq_msg_close (&part);
} while (more);
```
--------------------------------
### Set NDK Root and Version
Source: https://github.com/zeromq/libzmq/blob/master/builds/android/README.md
Manually export NDK_VERSION and ANDROID_NDK_ROOT when using a custom NDK installation. ANDROID_NDK_ROOT must be an absolute path.
```shell
export NDK_VERSION="android-ndk-r23b"
export ANDROID_NDK_ROOT=$HOME/${NDK_VERSION}
```
--------------------------------
### Polling indefinitely for input events
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_poll.adoc
This example demonstrates how to poll indefinitely for input events on both a 0MQ socket and a standard file descriptor. Ensure that the 'socket' and 'fd' variables are properly initialized before use.
```c
zmq_pollitem_t items [2];
/* First item refers to 0MQ socket 'socket' */
items[0].socket = socket;
items[0].events = ZMQ_POLLIN;
/* Second item refers to standard socket 'fd' */
items[1].socket = NULL;
items[1].fd = fd;
items[1].events = ZMQ_POLLIN;
/* Poll for events indefinitely */
int rc = zmq_poll (items, 2, -1);
assert (rc >= 0);
/* Returned events will be stored in items[].revents */
```
--------------------------------
### Get maximum number of sockets
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_ctx_get.adoc
Use ZMQ_MAX_SOCKETS to retrieve the maximum number of sockets allowed for this context. This example demonstrates setting and then getting this option.
```c
void *context = zmq_ctx_new ();
zmq_ctx_set (context, ZMQ_MAX_SOCKETS, 256);
int max_sockets = zmq_ctx_get (context, ZMQ_MAX_SOCKETS);
assert (max_sockets == 256);
```
--------------------------------
### Get blocky setting
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_ctx_get.adoc
Use ZMQ_BLOCKY to determine if the context will block on terminate. Returns 1 if blocking is enabled, 0 otherwise. This example shows how to disable the context deadlock gambit.
```c
zmq_ctx_set (ctx, ZMQ_BLOCKY, false);
```
```c
int blocky_setting = zmq_ctx_get (context, ZMQ_BLOCKY);
```
--------------------------------
### Build libzmq static and dynamic libraries
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
Use this command to build both the static library (libzmq.a) and the dynamic library (libzmq.so). Ensure you are in the zeromq-VERSION directory.
```bash
cd zeromq-VERSION
builds/zos/makelibzmq
```
--------------------------------
### Set SIGPIPE to be ignored
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
To handle potential SIGPIPE signals when sockets disconnect unexpectedly on z/OS, which does not support SO_NOSIGPIPE or MSG_NOSIGNAL, install this signal handler at the start of your application. This prevents SIGPIPE from terminating the application.
```c
#include
...
signal(SIGPIPE, SIG_IGN);
```
--------------------------------
### ZMQ Socket Monitor Example
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_socket_monitor.adoc
Demonstrates setting up socket monitoring for client and server sockets. It shows how to correctly use zmq_socket_monitor with 'inproc://' endpoints and verifies expected events. Note that monitoring over TCP is not supported and will result in EPROTONOSUPPORT.
```c
int main (void)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
// We'll monitor these two sockets
void *client = zmq_socket (ctx, ZMQ_DEALER);
assert (client);
void *server = zmq_socket (ctx, ZMQ_DEALER);
assert (server);
// Socket monitoring only works over inproc://
int rc = zmq_socket_monitor (client, "tcp://127.0.0.1:9999", 0);
assert (rc == -1);
assert (zmq_errno () == EPROTONOSUPPORT);
// Monitor all events on client and server sockets
rc = zmq_socket_monitor (client, "inproc://monitor-client", ZMQ_EVENT_ALL);
assert (rc == 0);
rc = zmq_socket_monitor (server, "inproc://monitor-server", ZMQ_EVENT_ALL);
assert (rc == 0);
// Create two sockets for collecting monitor events
void *client_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (client_mon);
void *server_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (server_mon);
// Connect these to the inproc endpoints so they'll get events
rc = zmq_connect (client_mon, "inproc://monitor-client");
assert (rc == 0);
rc = zmq_connect (server_mon, "inproc://monitor-server");
assert (rc == 0);
// Now do a basic ping test
rc = zmq_bind (server, "tcp://127.0.0.1:9998");
assert (rc == 0);
rc = zmq_connect (client, "tcp://127.0.0.1:9998");
assert (rc == 0);
bounce (client, server);
// Close client and server
close_zero_linger (client);
close_zero_linger (server);
// Now collect and check events from both sockets
int event = get_monitor_event (client_mon, NULL, NULL);
if (event == ZMQ_EVENT_CONNECT_DELAYED)
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED);
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
event = get_monitor_event (client_mon, NULL, NULL);
assert (event == ZMQ_EVENT_MONITOR_STOPPED);
// This is the flow of server events
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_LISTENING);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_ACCEPTED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_HANDSHAKE_SUCCEEDED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CLOSED);
event = get_monitor_event (server_mon, NULL, NULL);
assert (event == ZMQ_EVENT_MONITOR_STOPPED);
// Close down the sockets
close_zero_linger (client_mon);
close_zero_linger (server_mon);
zmq_ctx_term (ctx);
return 0 ;
}
```
--------------------------------
### Create a function to run the proxy
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_proxy_steerable.adoc
Sets up frontend, backend, and control sockets for the steerable proxy. Ensure sockets are bound before calling zmq_proxy_steerable.
```c
// Create the frontend and backend sockets to be proxied
void *frontend = zmq_socket (context, ZMQ_ROUTER);
void *backend = zmq_socket (context, ZMQ_DEALER);
// Create the proxy control socket
void *control = zmq_socket (context, ZMQ_REP);
// Bind the sockets.
zmq_bind (frontend, "tcp://*:5555");
zmq_bind (backend, "tcp://*:5556");
zmq_bind (control, "tcp://*:5557");
zmq_proxy_steerable(frontend, backend, NULL, control);
```
--------------------------------
### Build and run core tests
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
After building the libraries, use these commands to build and execute the core ZeroMQ tests. Ensure you are in the zeromq-VERSION directory.
```bash
cd zeromq-VERSION
builds/zos/maketests
builds/zos/runtests
```
--------------------------------
### zmq_getsockopt Example
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_getsockopt.adoc
Example of retrieving the high water mark for outgoing messages using zmq_getsockopt.
```APIDOC
## EXAMPLE
.Retrieving the high water mark for outgoing messages
----
/* Retrieve high water mark into sndhwm */
int sndhwm;
size_t sndhwm_size = sizeof (sndhwm);
rc = zmq_getsockopt (socket, ZMQ_SNDHWM, &sndhwm, &sndhwm_size);
assert (rc == 0);
----
```
--------------------------------
### Create and Destroy ZeroMQ Context
Source: https://context7.com/zeromq/libzmq/llms.txt
Demonstrates creating a ZeroMQ context, tuning its I/O threads and socket limits, and then terminating it cleanly. Use `zmq_ctx_new` to create a context and `zmq_ctx_term` to shut it down.
```c
#include
#include
#include
int main (void)
{
/* Create context — starts background I/O threads */
void *ctx = zmq_ctx_new ();
assert (ctx);
/* Tune the context before creating sockets */
/* Use 4 I/O threads for high-throughput workloads */
zmq_ctx_set (ctx, ZMQ_IO_THREADS, 4);
/* Raise the per-context socket limit from the default 1023 */
zmq_ctx_set (ctx, ZMQ_MAX_SOCKETS, 4096);
/* Verify settings */
int io_threads = zmq_ctx_get (ctx, ZMQ_IO_THREADS);
printf ("I/O threads: %d\n", io_threads); /* I/O threads: 4 */
/* ... create sockets and do work ... */
/* Cleanly shut down: waits for open sockets to close */
int rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
}
```
--------------------------------
### Create HTTP Server with ZMQ_STREAM
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_socket.adoc
This example demonstrates how to create a simple HTTP server using a ZMQ_STREAM socket. It involves setting up the context and socket, binding to a TCP port, and then entering a loop to receive HTTP requests and send responses. The code handles routing IDs and message framing specific to ZMQ_STREAM.
```c
void *ctx = zmq_ctx_new ();
assert (ctx);
/* Create ZMQ_STREAM socket */
void *socket = zmq_socket (ctx, ZMQ_STREAM);
assert (socket);
int rc = zmq_bind (socket, "tcp://*:8080");
assert (rc == 0);
/* Data structure to hold the ZMQ_STREAM routing id */
uint8_t routing_id [256];
size_t routing_id_size = 256;
/* Data structure to hold the ZMQ_STREAM received data */
uint8_t raw [256];
size_t raw_size = 256;
while (1) {
/* Get HTTP request; routing id frame and then request */
routing_id_size = zmq_recv (socket, routing_id, 256, 0);
assert (routing_id_size > 0);
do {
raw_size = zmq_recv (socket, raw, 256, 0);
assert (raw_size >= 0);
} while (raw_size == 256);
/* Prepares the response */
char http_response [] =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello, World!";
/* Sends the routing id frame followed by the response */
zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
zmq_send (socket, http_response, strlen (http_response), 0);
/* Closes the connection by sending the routing id frame followed by a zero response */
zmq_send (socket, routing_id, routing_id_size, ZMQ_SNDMORE);
zmq_send (socket, 0, 0, 0);
}
zmq_close (socket);
zmq_ctx_destroy (ctx);
```
--------------------------------
### Compile and link application with static library
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
When statically linking, compile your application and link it with the libzmq.a library. Ensure include files are in your compiler's include path and the library is in your library search path. The -lzmq flag links against the ZeroMQ library.
```c++
c++ -Wc,xplink -Wl,xplink ... -+ -o myprog myprog.cpp -lzmq
```
--------------------------------
### Set CMake Config Install Directory for Windows
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Sets the installation directory for CMake configuration files on Windows when not using MinGW. The path is set to 'CMake'.
```cmake
set(ZEROMQ_CMAKECONFIG_INSTALL_DIR
"CMake"
CACHE STRING "install path for ZeroMQConfig.cmake")
```
--------------------------------
### Compile and link application with dynamic library
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
For dynamic linking, first compile your application object file, then link it against the import file (libzmq.x). Ensure include files are in your compiler's include path and the shared object (libzmq.so) is in your LIBPATH at runtime.
```c++
c++ -Wc,xplink -Wc,dll ... -+ -c -o myprog.o myprog.cpp
c++ -Wl,xplink -o myprog myprog.o /PATH/TO/libzmq.x
```
--------------------------------
### Define CPack Components and Install Types
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Includes the CPack module and defines various components and installation types for the package. Components can be grouped, have display names, and be enabled or disabled.
```cmake
include(CPack)
cpack_add_component_group(Development DISPLAY_NAME "ZeroMQ software development kit" EXPANDED)
cpack_add_component(PerfTools DISPLAY_NAME "ZeroMQ performance tools" INSTALL_TYPES FullInstall DevInstall)
cpack_add_component(SourceCode DISPLAY_NAME "ZeroMQ source code" DISABLED INSTALL_TYPES FullInstall)
cpack_add_component(
SDK
DISPLAY_NAME
"ZeroMQ headers and libraries"
INSTALL_TYPES
FullInstall
DevInstall
GROUP
Development)
if(WITH_DOC)
cpack_add_component(
RefGuide
DISPLAY_NAME
"ZeroMQ reference guide"
INSTALL_TYPES
FullInstall
DevInstall
GROUP
Development)
endif()
cpack_add_component(
Runtime
DISPLAY_NAME
"ZeroMQ runtime files"
REQUIRED
INSTALL_TYPES
FullInstall
DevInstall
MinInstall)
cpack_add_install_type(FullInstall DISPLAY_NAME "Full install, including source code")
cpack_add_install_type(DevInstall DISPLAY_NAME "Developer install, headers and libraries")
cpack_add_install_type(MinInstall DISPLAY_NAME "Minimal install, runtime only")
```
--------------------------------
### Set CMake Config Install Directory for Non-Windows
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Sets the installation directory for CMake configuration files on non-Windows systems. It uses the library directory and 'cmake' prefix, following CMake search path conventions.
```cmake
set(ZEROMQ_CMAKECONFIG_INSTALL_DIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
CACHE STRING "install path for ZeroMQConfig.cmake")
```
--------------------------------
### Build Benchmark Radix Tree
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Builds an executable for benchmarking the radix tree implementation. It links against the static libzmq and includes the source directory for headers.
```cmake
add_executable(benchmark_radix_tree perf/benchmark_radix_tree.cpp)
target_link_libraries(benchmark_radix_tree libzmq-static)
target_include_directories(benchmark_radix_tree PUBLIC "${CMAKE_CURRENT_LIST_DIR}/src")
```
--------------------------------
### Get ZAP authenticated user ID for a message
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_msg_gets.adoc
Use this snippet to retrieve the ZAP authenticated user ID from a received message. Ensure the message is successfully received before attempting to get the user ID. The returned user ID is a UTF8-encoded string owned by the message.
```c
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, dealer, 0);
assert (rc != -1);
const char *user_id = zmq_msg_gets (&msg, ZMQ_MSG_PROPERTY_USER_ID);
zmq_msg_close (&msg);
```
--------------------------------
### Set CXXFLAGS for z/OS build
Source: https://github.com/zeromq/libzmq/blob/master/builds/zos/README.md
Enable XPLINK for the C++ compiler on z/OS.
```bash
setenv CXXFLAGS "-Wc,xplink -Wl,xplink -+"
```
--------------------------------
### Get IPv6 option
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_ctx_get.adoc
Use ZMQ_IPV6 to retrieve the IPv6 option for the context.
```c
int ipv6_option = zmq_ctx_get (context, ZMQ_IPV6);
```
--------------------------------
### Receiving a multi-frame message with zmq_msg_get
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_msg_get.adoc
This example demonstrates how to receive a multi-frame message by repeatedly calling zmq_msg_recv and checking the ZMQ_MORE property using zmq_msg_get. It continues to receive frames until ZMQ_MORE returns false, indicating the end of the message.
```c
zmq_msg_t frame;
while (true) {
// Create an empty 0MQ message to hold the message frame
int rc = zmq_msg_init (&frame);
assert (rc == 0);
// Block until a message is available to be received from socket
rc = zmq_msg_recv (socket, &frame, 0);
assert (rc != -1);
if (zmq_msg_get (&frame, ZMQ_MORE))
fprintf (stderr, "more\n");
else {
fprintf (stderr, "end\n");
break;
}
zmq_msg_close (&frame);
}
```
--------------------------------
### ZMQ_PRIORITY: Retrieve Priority
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_getsockopt.adoc
Gets the protocol-defined priority for all packets to be sent on this socket, where supported by the OS.
```APIDOC
## ZMQ_PRIORITY: Retrieve Priority
### Description
Gets the protocol-defined priority for all packets to be sent on this socket, where supported by the OS.
### Option Value Type
int
### Option Value Unit
>0
### Default Value
0
### Applicable Socket Types
all, only for connection-oriented transports
```
--------------------------------
### Get number of I/O threads
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_ctx_get.adoc
Use ZMQ_IO_THREADS to retrieve the size of the thread pool for the context.
```c
int io_threads = zmq_ctx_get (context, ZMQ_IO_THREADS);
```
--------------------------------
### Fill and Send a Message
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_sendmsg.adoc
Initializes a message with a specific size, fills it with data, and sends it to the socket. Asserts successful initialization and sending.
```c
/* Create a new message, allocating 6 bytes for message content */
zmq_msg_t msg;
int rc = zmq_msg_init_size (&msg, 6);
assert (rc == 0);
/* Fill in message content with 'AAAAAA' */
memset (zmq_msg_data (&msg), 'A', 6);
/* Send the message to the socket */
rc = zmq_sendmsg (socket, &msg, 0);
assert (rc == 6);
```
--------------------------------
### Initialize and Receive a Message
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_msg_init.adoc
Initializes an empty message structure and then receives a message from a socket. Ensure the return code is checked for successful initialization and reception.
```c
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
int nbytes = zmq_msg_recv (socket, &msg, 0);
assert (nbytes != -1);
```
--------------------------------
### ZMQ_NORM_BUFFER_SIZE
Source: https://github.com/zeromq/libzmq/blob/master/doc/zmq_getsockopt.adoc
Gets the NORM buffer size for NORM transport sender, receiver, and stream. Default is 2048 kilobytes.
```APIDOC
## ZMQ_NORM_BUFFER_SIZE
### Description
Retrieve NORM buffer size. Gets NORM buffer size for NORM transport sender, receiver, and stream.
NOTE: in DRAFT state, not yet available in stable releases.
### Option Value Type
int
### Option Value Unit
kilobytes
### Default Value
2048
### Applicable Socket Types
All, when using NORM transport.
```
--------------------------------
### Copy Perf-tools into Framework
Source: https://github.com/zeromq/libzmq/blob/master/CMakeLists.txt
Copies performance tool binaries into the ZeroMQ framework bundle. This custom command is executed after the target is built, specifically for macOS framework builds.
```cmake
add_custom_command(
TARGET libzmq
${perf-tool} POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy "$"
"${LIBRARY_OUTPUT_PATH}/ZeroMQ.framework/Versions/${ZMQ_VERSION_STRING}/MacOS/${perf-tool}"
VERBATIM
COMMENT "Perf tools")
```