Networking Reference
In-Depth Information
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_SUB);
const char* filter;
filter = "Company1|";
printf("Collecting stock information from the server.\n");
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, filter,
strlen(filter));
int i = 0;
for(i = 0; i < 10; i++) {
zmq_msg_t reply;
zmq_msg_init(&reply);
zmq_msg_recv(&reply, subscriber, 0);
int length = zmq_msg_size(&reply);
char* value = malloc(length + 1);
memcpy(value, zmq_msg_data(&reply), length);
zmq_msg_close(&reply);
printf("%s\n", value);
free(value);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
Now we can see the results as expected after the changes we have made in our
publisher and subscriber code.
The socket options
Since we use the publish-subscribe pattern, the option name we use
is ZMQ_SUBSCRIBE .
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, option_value,
strlen(option_value));
 
Search MirCeyron ::




Custom Search