2
Copyright (c) 2010-2011 250bpm s.r.o.
3
Copyright (c) 2011 VMware, Inc.
4
Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
6
This file is part of 0MQ.
8
0MQ is free software; you can redistribute it and/or modify it under
9
the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
26
#include "../include/zmq.h"
28
int main (int argc, char *argv [])
30
fprintf (stderr, "test_reqrep_device running...\n");
32
void *ctx = zmq_init (1);
35
// Create a req/rep device.
36
void *xreq = zmq_socket (ctx, ZMQ_XREQ);
38
int rc = zmq_bind (xreq, "tcp://127.0.0.1:5560");
40
void *xrep = zmq_socket (ctx, ZMQ_XREP);
42
rc = zmq_bind (xrep, "tcp://127.0.0.1:5561");
46
void *rep = zmq_socket (ctx, ZMQ_REP);
48
rc = zmq_connect (rep, "tcp://127.0.0.1:5560");
52
void *req = zmq_socket (ctx, ZMQ_REQ);
54
rc = zmq_connect (req, "tcp://127.0.0.1:5561");
58
rc = zmq_send (req, "ABC", 3, ZMQ_SNDMORE);
60
rc = zmq_send (req, "DEF", 3, 0);
63
// Pass the request through the device.
64
for (int i = 0; i != 4; i++) {
66
rc = zmq_msg_init (&msg);
68
rc = zmq_recvmsg (xrep, &msg, 0);
71
size_t sz = sizeof (rcvmore);
72
rc = zmq_getsockopt (xrep, ZMQ_RCVMORE, &rcvmore, &sz);
74
rc = zmq_sendmsg (xreq, &msg, rcvmore ? ZMQ_SNDMORE : 0);
78
// Receive the request.
80
rc = zmq_recv (rep, buff, 3, 0);
82
assert (memcmp (buff, "ABC", 3) == 0);
84
size_t sz = sizeof (rcvmore);
85
rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
88
rc = zmq_recv (rep, buff, 3, 0);
90
assert (memcmp (buff, "DEF", 3) == 0);
91
rc = zmq_getsockopt (rep, ZMQ_RCVMORE, &rcvmore, &sz);
96
rc = zmq_send (rep, "GHI", 3, ZMQ_SNDMORE);
98
rc = zmq_send (rep, "JKL", 3, 0);
101
// Pass the reply through the device.
102
for (int i = 0; i != 4; i++) {
104
rc = zmq_msg_init (&msg);
106
rc = zmq_recvmsg (xreq, &msg, 0);
109
rc = zmq_getsockopt (xreq, ZMQ_RCVMORE, &rcvmore, &sz);
111
rc = zmq_sendmsg (xrep, &msg, rcvmore ? ZMQ_SNDMORE : 0);
115
// Receive the reply.
116
rc = zmq_recv (req, buff, 3, 0);
118
assert (memcmp (buff, "GHI", 3) == 0);
119
rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
122
rc = zmq_recv (req, buff, 3, 0);
124
assert (memcmp (buff, "JKL", 3) == 0);
125
rc = zmq_getsockopt (req, ZMQ_RCVMORE, &rcvmore, &sz);
130
rc = zmq_close (req);
132
rc = zmq_close (rep);
134
rc = zmq_close (xrep);
136
rc = zmq_close (xreq);