Networking Reference
In-Depth Information
int main (int argc, char const *argv[]) {
void* context = zmq_ctx_new();
void* subscriber = zmq_socket(context, ZMQ_SUB);
printf("Collecting stock information from the server.\n");
int conn = zmq_connect(subscriber, "tcp://localhost:4040");
conn = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, 0, 0);
int i;
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);
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;
}
Setting a subscription using zmq_setsockopt(3) and subscribe is mandatory
whenever you use a SUB socket, otherwise you will not receive any messages.
This is a very common error.
The subscriber can set numerous subscriptions, which the subscriber receives,
to any messages if an update matches any of the subscriptions. It can unsubscribe
from particular subscriptions as well. Subscriptions are fixed-length blobs.
A subscriber receives the message using zmq_msg_recv(3) . zmq_msg_recv(3)
receives a message from a socket and stores the message. Previous messages,
if any, are deallocated.
int zmq_msg_recv (zmq_msg_t *msg, void *socket, int flags);
 
Search MirCeyron ::




Custom Search