[zeromq-dev] ZMQ_STREAM identity problem

Pieter Hintjens ph at imatix.com
Wed Oct 30 12:50:16 CET 2013

You should not treat the identity generated by a ROUTER socket as a
string, it will always be a null byte followed by some binary data.

Instead, read the identity frame off the socket, and then send it back
as a frame.

Also, don't try to get the identity as a socket option. That is for
setting the identity on a socket talking to a router. The identity of
any peer comes as the first frame in every message from that peer.

On Wed, Oct 30, 2013 at 12:20 PM, Chris Laws <clawsicus at gmail.com> wrote:
> I am investigating bridging applications that will allow non-zmq parts of my
> system to communicate with the ZMQ parts. Consequently I am playing around
> with ZMQ_STREAM sockets in both client and server scenarios.
> I have run into a problem when using CZMQ because the zsocket_identity
> function is returning a char* for what is really just some binary identity
> data. When I try to place this into the first zframe (used by the ZMQ_STREAM
> socket to route the message) I use something along the lines of:
> rc = zsocket_connect (sock, "tcp://");
> char *identity = zsocket_identity (sock);
> and then later:
> // Supply ZMQ with the identity to route the message to.
> frame = zframe_new (identity, strlen(identity));
> zframe_send (&frame, sock, ZMQ_SNDMORE);
> The strlen is returning 0 because the identity is really binary and often
> (for me at least) the first char is 0 which causes strlen to return 0.
> Consequently the identity passed in the zframe is bogus and the message is
> dropped.
> Can anyone suggest a better way to achieve this. I'm trying to use CZMQ
> because I like and prefer it but I guess I could mix in libzmq calls such
> as:
> // get the identity old-school style
> uint8_t id [256];
> size_t id_size = 256;
> rc = zmq_getsockopt (sock, ZMQ_IDENTITY, id, &id_size);
> which would provide the correct size when I create the frame holding the
> identity.
> Alternatively, perhaps the zsocket_identity CZMQ function could be changed
> so that a size_t is passed into the function and gets updated with the
> actual identity size?
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Pieter Hintjens
CEO of iMatix.com
Founder of ZeroMQ community
blog: http://hintjens.com

More information about the zeromq-dev mailing list