Advertisement
Guest User

zeromq multicast test

a guest
Oct 18th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <zmq.h>
  6.  
  7. //const char *ADDRESS = "tcp://127.0.0.1:21000";
  8. const char *ADDRESS = "epgm://10.0.0.103;239.0.0.1:5000";
  9.  
  10. #define HANDLE_ERROR(ret) (handle_error((ret), __FILE__, __LINE__))
  11.  
  12. void handle_error(int ret, char *file, int line)
  13. {
  14.     if (ret == 0)
  15.         return;
  16.  
  17.     printf("ERROR (%s:%d) -- %s\n", file, line, strerror(errno));
  18.     assert(0);
  19. }
  20.  
  21.  
  22. void run_client()
  23. {
  24.     int ret;
  25.  
  26.     void *context = zmq_init(1);
  27.     void *recvSocket = NULL;
  28.    
  29.     recvSocket = zmq_socket(context, ZMQ_SUB);
  30.     assert(recvSocket);
  31.    
  32.     // Set a filter to receive all subscribed data.
  33.     ret = zmq_setsockopt(recvSocket, ZMQ_SUBSCRIBE, "", 0);
  34.     HANDLE_ERROR(ret);
  35.  
  36.     ret = zmq_connect(recvSocket, ADDRESS);
  37.     HANDLE_ERROR(ret);
  38.  
  39.     while (1)
  40.     {
  41.         zmq_msg_t message;
  42.         zmq_msg_init(&message);
  43.  
  44.         int size = zmq_recvmsg(recvSocket, &message, 0);
  45.  
  46.         char *string = zmq_msg_data(&message);
  47.         printf("%s size %d\n", string, size);
  48.  
  49.         zmq_msg_close(&message);
  50.     }
  51.  
  52.     zmq_close(recvSocket);
  53.     zmq_term(context);
  54. }
  55.  
  56.  
  57. void run_server()
  58. {
  59.     int   ret;
  60.     void *context = zmq_init(1);
  61.     void *sendSocket = NULL;
  62.    
  63.     sendSocket = zmq_socket(context, ZMQ_PUB);
  64.     assert(sendSocket);
  65.  
  66.     //ret = zmq_bind(sendSocket, ADDRESS);
  67.     ret = zmq_connect(sendSocket, ADDRESS);
  68.     HANDLE_ERROR(ret);
  69.  
  70.     while (1) {
  71.         char buf[80] = "hello";
  72.  
  73.         zmq_msg_t message;
  74.         zmq_msg_init_size(&message, strlen(buf));
  75.         memcpy(zmq_msg_data(&message), buf, strlen(buf));
  76.  
  77.         ret = zmq_msg_send(&message, sendSocket, 0);
  78.         if (ret != strlen(buf))
  79.         {
  80.             HANDLE_ERROR(-1);
  81.         }
  82.  
  83.         zmq_msg_close(&message);
  84.     }
  85.  
  86.     zmq_close(sendSocket);
  87.     zmq_term(context);
  88. }
  89.  
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93.     printf("arg: %s\n", argv[1]);
  94.  
  95.     if (argc != 2) {
  96.         printf("ERROR: 1 argument expected!\n");
  97.         return -1;
  98.     }
  99.  
  100.     if (strcmp(argv[1], "client") == 0) {
  101.         printf("Running as client\n");
  102.         run_client();
  103.     }
  104.     else {
  105.         printf("Running as server\n");
  106.         run_server();
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement