libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_client_server.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 "testutil.hpp"
31 
32 int main (void)
33 {
35  void *ctx = zmq_ctx_new ();
36  assert (ctx);
37 
38  void *server = zmq_socket (ctx, ZMQ_SERVER);
39  void *client = zmq_socket (ctx, ZMQ_CLIENT);
40 
41  int rc = zmq_bind (server, "inproc://test-client-server");
42  assert (rc == 0);
43 
44  rc = zmq_connect (client, "inproc://test-client-server");
45  assert (rc == 0);
46 
47  zmq_msg_t msg;
48  rc = zmq_msg_init_size (&msg, 1);
49  assert (rc == 0);
50 
51  char *data = (char *) zmq_msg_data (&msg);
52  data [0] = 1;
53 
54  rc = zmq_msg_send (&msg, client, ZMQ_SNDMORE);
55  assert (rc == -1);
56 
57  rc = zmq_msg_send (&msg, client, 0);
58  assert (rc == 1);
59 
60  rc = zmq_msg_init (&msg);
61  assert (rc == 0);
62 
63  rc = zmq_msg_recv (&msg, server, 0);
64  assert (rc == 1);
65 
66  uint32_t routing_id = zmq_msg_routing_id (&msg);
67  assert (routing_id != 0);
68 
69  rc = zmq_msg_close (&msg);
70  assert (rc == 0);
71 
72  rc = zmq_msg_init_size (&msg, 1);
73  assert (rc == 0);
74 
75  data = (char *)zmq_msg_data (&msg);
76  data[0] = 2;
77 
78  rc = zmq_msg_set_routing_id (&msg, routing_id);
79  assert (rc == 0);
80 
81  rc = zmq_msg_send (&msg, server, ZMQ_SNDMORE);
82  assert (rc == -1);
83 
84  rc = zmq_msg_send (&msg, server, 0);
85  assert (rc == 1);
86 
87  rc = zmq_msg_recv (&msg, client, 0);
88  assert (rc == 1);
89 
90  routing_id = zmq_msg_routing_id (&msg);
91  assert (routing_id == 0);
92 
93  rc = zmq_msg_close (&msg);
94  assert (rc == 0);
95 
96  rc = zmq_close (server);
97  assert (rc == 0);
98 
99  rc = zmq_close (client);
100  assert (rc == 0);
101 
102  rc = zmq_ctx_term (ctx);
103  assert (rc == 0);
104 
105  return 0 ;
106 }
uint32_t zmq_msg_routing_id(zmq_msg_t *msg)
Definition: zmq.cpp:715
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT void * zmq_msg_data(zmq_msg_t *msg)
Definition: zmq.cpp:666
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:629
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_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
#define ZMQ_SERVER
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
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
ZMQ_EXPORT int zmq_msg_init_size(zmq_msg_t *msg, size_t size)
Definition: zmq.cpp:618
int zmq_msg_set_routing_id(zmq_msg_t *msg, uint32_t routing_id)
Definition: zmq.cpp:710
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
int main(void)
#define ZMQ_CLIENT