Guest User

Untitled

a guest
May 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <zmq.hpp>
  3.  
  4. int main ()
  5. {
  6. try {
  7. // Initialise 0MQ context with one application and one I/O thread
  8. zmq::context_t ctx (1, 1);
  9. // Create a ZMQ_REQ socket to send requests and receive replies
  10. zmq::socket_t s (ctx, ZMQ_P2P);
  11. // Connect it to port 5555 on localhost using the TCP transport
  12. s.connect ("tcp://localhost:5555");
  13.  
  14. // Construct an example zmq::message_t with our query
  15. const char *query_string = "SELECT * FROM mytable";
  16. zmq::message_t query (strlen (query_string) + 1);
  17. memcpy (query.data (), query_string, strlen (query_string) + 1);
  18. // Send the query
  19. s.send (query);
  20.  
  21. // Receive and display the result
  22. zmq::message_t resultset;
  23. s.recv (&resultset);
  24. const char *resultset_string = (const char *)resultset.data ();
  25. printf ("Received response: '%s'\n", resultset_string);
  26. }
  27. catch (std::exception &e) {
  28. // 0MQ throws standard exceptions just like any other C++ API
  29. printf ("An error occurred: %s\n", e.what());
  30. return 1;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. this next bit doesn't work at all (comments out the recieve bit)
  44.  
  45.  
  46. #include <stdio.h>
  47. #include <zmq.hpp>
  48.  
  49. int main ()
  50. {
  51. try {
  52. // Initialise 0MQ context with one application and one I/O thread
  53. zmq::context_t ctx (1, 1);
  54. // Create a ZMQ_REQ socket to send requests and receive replies
  55. zmq::socket_t s (ctx, ZMQ_P2P);
  56. // Connect it to port 5555 on localhost using the TCP transport
  57. s.connect ("tcp://localhost:5555");
  58.  
  59. // Construct an example zmq::message_t with our query
  60. const char *query_string = "SELECT * FROM mytable";
  61. zmq::message_t query (strlen (query_string) + 1);
  62. memcpy (query.data (), query_string, strlen (query_string) + 1);
  63. // Send the query
  64. s.send (query);
  65.  
  66. // Receive and display the result
  67. /*zmq::message_t resultset;
  68. s.recv (&resultset);
  69. const char *resultset_string = (const char *)resultset.data ();
  70. printf ("Received response: '%s'\n", resultset_string);*/
  71. }
  72. catch (std::exception &e) {
  73. // 0MQ throws standard exceptions just like any other C++ API
  74. printf ("An error occurred: %s\n", e.what());
  75. return 1;
  76. }
  77.  
  78. return 0;
  79. }
Add Comment
Please, Sign In to add comment