Guest User

Untitled

a guest
Jan 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. int main (void)
  2. {
  3. Message protomsg = MESSAGE__INIT;
  4. void *buf;
  5. unsigned len;
  6.  
  7. void *context = zmq_ctx_new();
  8. void *subscriber = zmq_socket(context, ZMQ_PUB);
  9. zmq_bind(subscriber, "ipc://my.sock");
  10.  
  11. //Initialising protomsg (not so important)
  12.  
  13. //sending message
  14.  
  15. len = message__get_packed_size(&protomsg);
  16. buf = malloc(len);
  17. message__pack(&protomsg, buf);
  18.  
  19. zmq_msg_t output;
  20. zmq_msg_init_size(&output, len);
  21. zmq_msg_init_data(&output, buf, len, NULL, NULL);
  22. if(zmq_msg_send(&output, subscriber, 0) == -1)
  23. perror("Error sending message n");
  24. else
  25. printf("Message successfully sent n");
  26. zmq_msg_close(&output);
  27. free(buf);
  28.  
  29. zmq_close (subscriber);
  30. zmq_ctx_destroy (context);
  31. return 0;
  32. }
  33.  
  34. int main (void){
  35. Message *protomsg;
  36. void *context = zmq_ctx_new ();
  37. void *publisher = zmq_socket (context, ZMQ_SUB);
  38. zmq_connect(publisher, "ipc://my.sock");
  39. zmq_setsockopt(publisher, ZMQ_SUBSCRIBE, "", 0);
  40.  
  41. // Read packed message from ZMQ.
  42. zmq_msg_t msg;
  43. zmq_msg_init(&msg);
  44. if(zmq_msg_recv(&msg, publisher, 0) == -1)
  45. perror("Error receiving message n");
  46. else
  47. printf("Message received");
  48. memcpy((void *)protomsg, zmq_msg_data(&msg), zmq_msg_size(&msg));
  49.  
  50. // Unpack the message using protobuf-c.
  51. protomsg = message__unpack(NULL, zmq_msg_size(&msg), (void *)&data);
  52. if (protomsg == NULL)
  53. {
  54. fprintf(stderr, "error unpacking incoming messagen");
  55. exit(1);
  56. }
  57.  
  58. printf("Address: %u, Type: %u, Information[0]: %u, Information[1]: %u n", protomsg->address-48, protomsg->frametype, protomsg->information[0], protomsg->information[1]);
  59. zmq_msg_close (&msg);
  60.  
  61. // Free the unpacked message
  62. message__free_unpacked(protomsg, NULL);
  63.  
  64. //close context,socket..
  65. }
Add Comment
Please, Sign In to add comment