Advertisement
Guest User

Untitled

a guest
May 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <zmq.h>
  4.  
  5. int main(void) {
  6. //ctx = zmq.Context()
  7. void *context = zmq_ctx_new ();
  8. //pub_sock = ctx.socket(zmq.PUB)
  9. void *pub_socket = zmq_socket (context, ZMQ_PUB);
  10. //pub_sock.rcvhwm = 0
  11. int hwm = 0;
  12. zmq_setsockopt(pub_socket, ZMQ_RCVHWM, &hwm,sizeof(int));
  13. //pub_sock.bind('ipc://pub') ... using tcp instead of ipc
  14. zmq_bind (pub_socket, "tcp://*:5560");
  15. //sub_sock = ctx.socket(zmq.SUB)
  16. void *sub_socket = zmq_socket (context, ZMQ_SUB);
  17. //sub_sock.sndhwm = 0
  18. zmq_setsockopt(sub_socket, ZMQ_SNDHWM, &hwm,sizeof(int));
  19. //sub_sock.connect('ipc://pub')
  20. zmq_connect (sub_socket, "tcp://10.82.56.195:5560");
  21. //for n in range(0, 10000):
  22. // sub_sock.setsockopt(zmq.SUBSCRIBE, '%d\x00' % n)
  23. // ... using binary data instead of strings
  24. // .. used 1024 instead of 10,000
  25. for (int i = 0; i < 1024; i++)
  26. zmq_setsockopt(sub_socket, ZMQ_SUBSCRIBE, &i, 2);
  27. //time.sleep(2)
  28. sleep(2);
  29. //for n in range(0, 10000): .
  30. for (int i = 0 ; i < 1024; i++)
  31. {
  32. //pub_sock.send_multipart(['%d\x00' % n, '%d' % n]) ... not using multipart messages
  33. unsigned char out_message[7];
  34. out_message[0] = i%256;
  35. out_message[1] = i/256;
  36. snprintf((char*)&(out_message[2]),5,"%04x",i);
  37. zmq_send(pub_socket, out_message, sizeof(out_message)-1, 0);
  38. /* Original alternates send/recv
  39. * Uncomment this for failing case
  40. * }
  41. * for(int i = 0; i < 10000; i++)
  42. * {
  43. */
  44. //m = sub_sock.recv_multipart()
  45. zmq_msg_t message;
  46. zmq_msg_init (&message);
  47. int size = zmq_msg_recv(&message, sub_socket, 0);
  48. if (size == -1)
  49. zmq_msg_close(&message);
  50. else
  51. {
  52. unsigned char *the_message_data = (unsigned char*)zmq_msg_data(&message);
  53. zmq_msg_close(&message);
  54.  
  55. //print m[1] ... actually printing out complete message (including envelope)
  56. printf("%d: ", i);
  57. for (int j = 0; j < 6; j++)
  58. printf("%02x ", the_message_data[j]);
  59. printf("\n");
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement