Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main (void)
- {
- Message protomsg = MESSAGE__INIT;
- void *buf;
- unsigned len;
- void *context = zmq_ctx_new();
- void *subscriber = zmq_socket(context, ZMQ_PUB);
- zmq_bind(subscriber, "ipc://my.sock");
- //Initialising protomsg (not so important)
- //sending message
- len = message__get_packed_size(&protomsg);
- buf = malloc(len);
- message__pack(&protomsg, buf);
- zmq_msg_t output;
- zmq_msg_init_size(&output, len);
- zmq_msg_init_data(&output, buf, len, NULL, NULL);
- if(zmq_msg_send(&output, subscriber, 0) == -1)
- perror("Error sending message n");
- else
- printf("Message successfully sent n");
- zmq_msg_close(&output);
- free(buf);
- zmq_close (subscriber);
- zmq_ctx_destroy (context);
- return 0;
- }
- int main (void){
- Message *protomsg;
- void *context = zmq_ctx_new ();
- void *publisher = zmq_socket (context, ZMQ_SUB);
- zmq_connect(publisher, "ipc://my.sock");
- zmq_setsockopt(publisher, ZMQ_SUBSCRIBE, "", 0);
- // Read packed message from ZMQ.
- zmq_msg_t msg;
- zmq_msg_init(&msg);
- if(zmq_msg_recv(&msg, publisher, 0) == -1)
- perror("Error receiving message n");
- else
- printf("Message received");
- memcpy((void *)protomsg, zmq_msg_data(&msg), zmq_msg_size(&msg));
- // Unpack the message using protobuf-c.
- protomsg = message__unpack(NULL, zmq_msg_size(&msg), (void *)&data);
- if (protomsg == NULL)
- {
- fprintf(stderr, "error unpacking incoming messagen");
- exit(1);
- }
- printf("Address: %u, Type: %u, Information[0]: %u, Information[1]: %u n", protomsg->address-48, protomsg->frametype, protomsg->information[0], protomsg->information[1]);
- zmq_msg_close (&msg);
- // Free the unpacked message
- message__free_unpacked(protomsg, NULL);
- //close context,socket..
- }
Add Comment
Please, Sign In to add comment