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