Pathologic Subscriber in C

// Pathological subscriber
// Subscribes to one random topic and prints received messages

#include "czmq.h"

int main (int argc, char *argv [])
zctx_t *context = zctx_new ();
void *subscriber = zsocket_new (context, ZMQ_SUB);
if (argc == 2)
zsocket_connect (subscriber, argv [1]);
zsocket_connect (subscriber, "tcp://localhost:5556");

srandom ((unsigned) time (NULL));
char subscription [5];
sprintf (subscription, "%03d", randof (1000));
zsocket_set_subscribe (subscriber, subscription);

while (true) {
char *topic = zstr_recv (subscriber);
if (!topic)
char *data = zstr_recv (subscriber);
assert (streq (topic, subscription));
puts (data);
free (topic);
free (data);
zctx_destroy (&context);
return 0;