zsocket(3)

zsocket(3)

CZMQ Manual - CZMQ/1.4.1

Name

zsocket - working with ØMQ sockets

Synopsis

//  This port range is defined by IANA for dynamic or private ports
//  We use this when choosing a port for dynamic binding.
#define ZSOCKET_DYNFROM     0xc000
#define ZSOCKET_DYNTO       0xffff

//  Create a new socket within our CZMQ context, replaces zmq_socket.
//  Use this to get automatic management of the socket at shutdown.
//  Note: SUB sockets do not automatically subscribe to everything; you
//  must set filters explicitly.
CZMQ_EXPORT void *
    zsocket_new (zctx_t *self, int type);

//  Destroy a socket within our CZMQ context, replaces zmq_close.
CZMQ_EXPORT void
    zsocket_destroy (zctx_t *self, void *socket);

//  Bind a socket to a formatted endpoint. If the port is specified as
//  '*', binds to any free port from ZSOCKET_DYNFROM to ZSOCKET_DYNTO
//  and returns the actual port number used. Otherwise asserts that the
//  bind succeeded with the specified port number. Always returns the
//  port number if successful.
CZMQ_EXPORT int
    zsocket_bind (void *socket, const char *format, ...);

//  Connect a socket to a formatted endpoint
//  Returns 0 if OK, -1 if the endpoint was invalid.
CZMQ_EXPORT int
    zsocket_connect (void *socket, const char *format, ...);

//  Disonnect a socket from a formatted endpoint
//  Returns 0 if OK, -1 if the endpoint was invalid or the function
//  isn't supported.
CZMQ_EXPORT int
    zsocket_disconnect (void *socket, const char *format, ...);

//  Poll for input events on the socket. Returns TRUE if there is input
//  ready on the socket, else FALSE.
CZMQ_EXPORT bool
    zsocket_poll (void *socket, int msecs);

//  Returns socket type as printable constant string
CZMQ_EXPORT char *
    zsocket_type_str (void *socket);

//  Self test of this class
CZMQ_EXPORT int
    zsocket_test (bool verbose);

Description

The zsocket class provides helper functions for ØMQ sockets. It doesn't wrap the ØMQ socket type, to avoid breaking all libzmq socket-related calls.

Example

From zsocket_test method

 zctx_t *ctx = zctx_new ();
 assert (ctx);

 // Create a detached thread, let it run
 char *interf = "*";
 char *domain = "localhost";
 int service = 5560;

 void *writer = zsocket_new (ctx, ZMQ_PUSH);
 assert (writer);
 void *reader = zsocket_new (ctx, ZMQ_PULL);
 assert (reader);
 assert (streq (zsocket_type_str (writer), "PUSH"));
 assert (streq (zsocket_type_str (reader), "PULL"));
 int rc = zsocket_bind (writer, "tcp://%s:%d", interf, service);
 assert (rc == service);
 rc = zsocket_connect (reader, "tcp://%s:%d", domain, service);
 assert (rc == 0);
 zstr_send (writer, "HELLO");
 char *message = zstr_recv (reader);
 assert (message);
 assert (streq (message, "HELLO"));
 free (message);

 int port = zsocket_bind (writer, "tcp://%s:*", interf);
 assert (port >= ZSOCKET_DYNFROM && port <= ZSOCKET_DYNTO);

 assert (zsocket_poll (writer, 100) == false);

 rc = zsocket_connect (reader, "txp://%s:%d", domain, service);
 assert (rc == -1);

 zsocket_destroy (ctx, writer);  zctx_destroy (&ctx);

See also

czmq(7)

Authors

The CZMQ manual was written by Pieter Hintjens<moc.xitami|hp#moc.xitami|hp>.

Resources

Main web site: http://czmq.zeromq.org/

Report bugs to the ØMQ development mailing list: <gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez>

Copyright

Copyright (c) 1991-2010 iMatix Corporation and contributors. License LGPLv3+: GNU LGPL 3 or later <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to change it and redistribute it. There is NO WARRANTY, to the extent permitted by law. For details see the files COPYING and COPYING.LESSER included with the CZMQ distribution.