ZSTR(3)

✨ zstr β€” the class for sending and receiving strings ✨

okay bestie, welcome to ZSTR(3), from the CZMQ Manual πŸ’– this is the little class that makes strings go zoom across your 0MQ sockets, and honestly? she's iconic for it.


🌸 SYNOPSIS 🌸

//  This is a stable class, and may not change except for emergencies. It
//  is provided in stable builds.
//  This class has draft methods, which may change over time. They are not
//  in stable releases, by default. Use --enable-drafts to enable.
//  Receive C string from socket. Caller must free returned string using
//  zstr_free(). Returns NULL if the context is being terminated or the
//  process was interrupted.
//  Caller owns return value and must destroy it when done.
CZMQ_EXPORT char *
    zstr_recv (void *source);

//  Receive a series of strings (until NULL) from multipart data.
//  Each string is allocated and filled with string data; if there
//  are not enough frames, unallocated strings are set to NULL.
//  Returns -1 if the message could not be read, else returns the
//  number of strings filled, zero or more. Free each returned string
//  using zstr_free(). If not enough strings are provided, remaining
//  multipart frames in the message are dropped.
CZMQ_EXPORT int
    zstr_recvx (void *source, char **string_p, ...);

//  Send a C string to a socket, as a frame. The string is sent without
//  trailing null byte; to read this you can use zstr_recv, or a similar
//  method that adds a null terminator on the received string. String
//  may be NULL, which is sent as "".
CZMQ_EXPORT int
    zstr_send (void *dest, const char *string);

//  Send a C string to a socket, as zstr_send(), with a MORE flag, so that
//  you can send further strings in the same multi-part message.
CZMQ_EXPORT int
    zstr_sendm (void *dest, const char *string);

//  Send a formatted string to a socket. Note that you should NOT use
//  user-supplied strings in the format (they may contain '%' which
//  will create security holes).
CZMQ_EXPORT int
    zstr_sendf (void *dest, const char *format, ...) CHECK_PRINTF (2);

//  Send a formatted string to a socket, as for zstr_sendf(), with a
//  MORE flag, so that you can send further strings in the same multi-part
//  message.
CZMQ_EXPORT int
    zstr_sendfm (void *dest, const char *format, ...) CHECK_PRINTF (2);

//  Send a series of strings (until NULL) as multipart data
//  Returns 0 if the strings could be sent OK, or -1 on error.
CZMQ_EXPORT int
    zstr_sendx (void *dest, const char *string, ...);

//  Free a provided string, and nullify the parent pointer. Safe to call on
//  a null pointer.
CZMQ_EXPORT void
    zstr_free (char **string_p);

//  Self test of this class.
CZMQ_EXPORT void
    zstr_test (bool verbose);

#ifdef CZMQ_BUILD_DRAFT_API
//  *** Draft method, for development use, may change without warning ***
//  De-compress and receive C string from socket, received as a message
//  with two frames: size of the uncompressed string, and the string itself.
//  Caller must free returned string using zstr_free(). Returns NULL if the
//  context is being terminated or the process was interrupted.
//  Caller owns return value and must destroy it when done.
CZMQ_EXPORT char *
    zstr_recv_compress (void *source);

//  *** Draft method, for development use, may change without warning ***
//  Compress and send a C string to a socket, as a message with two frames:
//  size of the uncompressed string, and the string itself. The string is
//  sent without trailing null byte; to read this you can use
//  zstr_recv_compress, or a similar method that de-compresses and adds a
//  null terminator on the received string.
CZMQ_EXPORT int
    zstr_send_compress (void *dest, const char *string);

//  *** Draft method, for development use, may change without warning ***
//  Compress and send a C string to a socket, as zstr_send_compress(),
//  with a MORE flag, so that you can send further strings in the same
//  multi-part message.
CZMQ_EXPORT int
    zstr_sendm_compress (void *dest, const char *string);

//  *** Draft method, for development use, may change without warning ***
//  Accepts a void pointer and returns a fresh character string. If source
//  is null, returns an empty string.
//  Caller owns return value and must destroy it when done.
CZMQ_EXPORT char *
    zstr_str (void *source);

#endif // CZMQ_BUILD_DRAFT_API
Also, the page literally says this: Please add '@interface' section in './../src/zstr.c'. β€” so like, noted, someone go do that πŸ™ˆ

Let me break down the ✨vibes✨ for you real quick:

  • zstr_recv β€” pulls a C string off the socket. You, the caller, MUST free it with zstr_free(). Gives you NULL if the context is getting terminated or the process got interrupted. And yes, caller owns the return value, so destroy it when you're done, periodt πŸ’…
  • zstr_recvx β€” grabs a whole series of strings (stops at NULL) out of multipart data. Each string gets allocated and filled; if there aren't enough frames, the unallocated ones just get set to NULL. Returns -1 if the message couldn't be read, otherwise it hands back the number of strings filled, zero or more. Free each one with zstr_free(). And heads up: if you don't provide enough strings, the leftover multipart frames in the message get dropped πŸ’€
  • zstr_send β€” sends a C string to a socket as a frame. No trailing null byte goes on the wire; to read it back you use zstr_recv, or something similar that adds a null terminator on receive. The string may be NULL, which gets sent as "".
  • zstr_sendm β€” same as zstr_send() but with a MORE flag, so you can keep sending strings in the same multi-part message πŸŽ€
  • zstr_sendf β€” sends a formatted string. ⚠️ And bestie, listen: do NOT put user-supplied strings in the format, because they might contain '%' and that creates security holes. We do not do that here.
  • zstr_sendfm β€” formatted send, like zstr_sendf(), but with a MORE flag so you can keep the multi-part message going.
  • zstr_sendx β€” sends a series of strings (until NULL) as multipart data. Returns 0 if it all went fine, -1 on error.
  • zstr_free β€” frees the string you give it and nullifies the parent pointer. Safe to call on a null pointer, so you don't have to vibe check it first 🫢
  • zstr_test β€” the self test for this class. Takes bool verbose.

And the draft girlies (only when CZMQ_BUILD_DRAFT_API is defined, i.e. draft API is enabled β€” they may change without warning, so don't get too attached 😭):

  • zstr_recv_compress β€” de-compresses and receives a C string from a socket, arriving as a message with two frames: the size of the uncompressed string, and the string itself. Caller must free with zstr_free(). Returns NULL if the context is being terminated or the process was interrupted. Caller owns the return value and must destroy it when done.
  • zstr_send_compress β€” compresses and sends a C string, as a message with two frames: the size of the uncompressed string, and the string itself. No trailing null byte on the wire; read it with zstr_recv_compress or similar, which de-compresses and adds a null terminator on the received string.
  • zstr_sendm_compress β€” compress-and-send like zstr_send_compress(), but with a MORE flag, so you can send more strings in the same multi-part message.
  • zstr_str β€” takes a void pointer and gives you a fresh character string. If source is null, you get an empty string. Caller owns the return value and must destroy it when done.

πŸ¦‹ DESCRIPTION πŸ¦‹

So the zstr class is your little utility squad for sending and receiving C strings across 0MQ sockets. She sends strings without a terminating null, and appends a null byte back onto received strings. Basically: this class is for simple message sending, and she's serving main character energy while doing it πŸ‘‘

Here's the whole vibe, wire vs. memory:

           Memory                       Wire
           +-------------+---+          +---+-------------+
    Send   | S t r i n g | 0 |  ---->   | 6 | S t r i n g |
           +-------------+---+          +---+-------------+

           Wire                         Heap
           +---+-------------+          +-------------+---+
    Recv   | 6 | S t r i n g |  ---->   | S t r i n g | 0 |
           +---+-------------+          +-------------+---+

Like, the 0 stays home when you send, and gets lovingly reattached when you receive. It's giving considerate πŸ₯Ή


🌈 EXAMPLE 🌈

Straight from the zstr_test method, no cap:

//  Create two PAIR sockets and connect over inproc
zsock_t *output = zsock_new_pair ("@inproc://zstr.test");
assert (output);
zsock_t *input = zsock_new_pair (">inproc://zstr.test");
assert (input);

//  Send ten strings, five strings with MORE flag and then END
int string_nbr;
for (string_nbr = 0; string_nbr < 10; string_nbr++)
    zstr_sendf (output, "this is string %d", string_nbr);
zstr_sendx (output, "This", "is", "almost", "the", "very", "END", NULL);

//  Read and count until we receive END
string_nbr = 0;
for (string_nbr = 0;; string_nbr++) {
    char *string = zstr_recv (input);
    assert (string);
    if (streq (string, "END")) {
        zstr_free (&string);
        break;
    }
    zstr_free (&string);
}
assert (string_nbr == 15);

#ifdef HAVE_LIBLZ4
int ret = zstr_send_compress (output, "loooong");
assert (ret == 0);
char *string = zstr_recv_compress (input);
assert (string);
assert (streq (string, "loooong"));
zstr_free (&string);

zstr_send_compress (output, "loooong");
assert (ret == 0);
zmsg_t *msg = zmsg_recv (input);
assert (msg);
assert (*((size_t *)zframe_data (zmsg_first (msg))) == strlen ("loooong"));
zmsg_destroy (&msg);
#endif

zsock_destroy (&input);
zsock_destroy (&output);

#if defined (ZMQ_SERVER)
//  Test SERVER/CLIENT over zstr
zsock_t *server = zsock_new_server ("inproc://zstr-test-routing");
zsock_t *client = zsock_new_client ("inproc://zstr-test-routing");;
assert (server);
assert (client);

//  Try normal ping-pong to check reply routing ID
int rc = zstr_send (client, "Hello");
assert (rc == 0);
char *request = zstr_recv (server);
assert (streq (request, "Hello"));
assert (zsock_routing_id (server));
freen (request);

rc = zstr_send (server, "World");
assert (rc == 0);
char *reply = zstr_recv (client);
assert (streq (reply, "World"));
freen (reply);

rc = zstr_sendf (server, "%s", "World");
assert (rc == 0);
reply = zstr_recv (client);
assert (streq (reply, "World"));
freen (reply);

//  Try ping-pong using sendx and recx
rc = zstr_sendx (client, "Hello", NULL);
assert (rc == 0);
rc = zstr_recvx (server, &request, NULL);
assert (rc >= 0);
assert (streq (request, "Hello"));
freen (request);

rc = zstr_sendx (server, "World", NULL);
assert (rc == 0);
rc = zstr_recvx (client, &reply, NULL);
assert (rc >= 0);
assert (streq (reply, "World"));
freen (reply);

//  Client and server disallow multipart
rc = zstr_sendm (client, "Hello");
assert (rc == -1);
rc = zstr_sendm (server, "World");
assert (rc == -1);

zsock_destroy (&client);
zsock_destroy (&server);
#endif

#if defined (__WINDOWS__)
zsys_shutdown();
#endif

πŸ’œ AUTHORS πŸ’œ

The czmq manual was written by the authors in the AUTHORS file. (Squad effort, love that for them.)


πŸŽ€ RESOURCES πŸŽ€

Main web site:

Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>


Copyright (c) the Contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for 0MQ: http://czmq.zeromq.org. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. LICENSE included with the czmq distribution.


⭐ NOTES ⭐

  1. zeromq-dev@lists.zeromq.org mailto:zeromq-dev@lists.zeromq.org

okay that's the whole page bestie, you ate it πŸ’… this was CZMQ 4.2.1, dated 01/16/2026, and it's ZSTR(3) β€” go forth and send your strings with confidence πŸ¦‹βœ¨

πŸ“¦ where this page comes from