receive.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <zmq.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. int main (int argc, char *argv [])
  29. {
  30. const char *connect_to;
  31. int message_count;
  32. int message_size;
  33. void *ctx;
  34. void *s;
  35. int rc;
  36. int i;
  37. zmq_msg_t msg;
  38. if (argc != 4) {
  39. printf ("usage: remote_thr <connect-to> <message-size> "
  40. "<message-count>\n");
  41. return 1;
  42. }
  43. connect_to = argv [1];
  44. message_size = atoi (argv [2]);
  45. message_count = atoi (argv [3]);
  46. ctx = zmq_init (1);
  47. if (!ctx) {
  48. printf ("error in zmq_init: %s\n", zmq_strerror (errno));
  49. return -1;
  50. }
  51. s = zmq_socket (ctx, ZMQ_PUSH);
  52. if (!s) {
  53. printf ("error in zmq_socket: %s\n", zmq_strerror (errno));
  54. return -1;
  55. }
  56. // Add your socket options here.
  57. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
  58. rc = zmq_connect (s, connect_to);
  59. if (rc != 0) {
  60. printf ("error in zmq_connect: %s\n", zmq_strerror (errno));
  61. return -1;
  62. }
  63. for (i = 0; i != message_count; i++) {
  64. rc = zmq_msg_init_size (&msg, message_size);
  65. if (rc != 0) {
  66. printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno));
  67. return -1;
  68. }
  69. rc = zmq_sendmsg (s, &msg, 0);
  70. if (rc < 0) {
  71. printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno));
  72. return -1;
  73. }
  74. rc = zmq_msg_close (&msg);
  75. if (rc != 0) {
  76. printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno));
  77. return -1;
  78. }
  79. }
  80. rc = zmq_close (s);
  81. if (rc != 0) {
  82. printf ("error in zmq_close: %s\n", zmq_strerror (errno));
  83. return -1;
  84. }
  85. rc = zmq_ctx_term (ctx);
  86. if (rc != 0) {
  87. printf ("error in zmq_ctx_term: %s\n", zmq_strerror (errno));
  88. return -1;
  89. }
  90. return 0;
  91. }