Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. /**
  2. * Run Client of PAIR Pattern.
  3. *
  4. * $ clang++ pair_client.cpp -lzmq -o pair_client.exe
  5. */
  6. #include <zmq.hpp>
  7. #include <chrono>
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <unistd.h>
  11. #include <sys/wait.h>
  12.  
  13. int s_send(void *socket, char *string)
  14. {
  15. int size = zmq_send(socket, string, strlen(string), 0);
  16. return size;
  17. }
  18.  
  19. int s_recv(void *socket, char buffer[])
  20. {
  21. int size = zmq_recv(socket, buffer, 255, 0);
  22. return size;
  23. }
  24.  
  25. int run(void)
  26. {
  27. std::cout << "Starting PAIR Client.\n";
  28. void* context = zmq_ctx_new();
  29. void* subscriber = zmq_socket(context, ZMQ_PAIR);
  30. {
  31. auto const rc = zmq_connect(subscriber, "tcp://localhost:5556");
  32. assert(rc == 0);
  33. }
  34. char buffer[255] = {'\0'};
  35. int const count = 10;
  36. for (int i = 0; i < count; ++i) {
  37. s_recv(subscriber, buffer);
  38. buffer[10] = '\0';
  39. std::cout << "Received message=\"" << buffer << "\"\n";
  40. }
  41. zmq_close(subscriber);
  42. zmq_ctx_destroy(context);
  43. std::cout << "Finished.\n";
  44. return 0;
  45. }
  46.  
  47. int main(void)
  48. {
  49. run();
  50. return EXIT_SUCCESS;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement