Skip to content

Commit c9225c1

Browse files
committed
Add a more complex example involving multi-part messages
1 parent 505edeb commit c9225c1

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ Supported platforms
4141

4242
Examples
4343
========
44-
This example requires at least C++11.
44+
These examples requires at least C++11.
4545
```c++
46-
#include <string>
4746
#include <zmq.hpp>
4847

4948
int main()
@@ -54,6 +53,36 @@ int main()
5453
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
5554
}
5655
```
56+
This a more complex example where we send and receive multi-part messages.
57+
```c++
58+
#include <iostream>
59+
#include <zmq_addon.hpp>
60+
61+
int main()
62+
{
63+
zmq::context_t ctx;
64+
zmq::socket_t sock1(ctx, zmq::socket_type::pair);
65+
zmq::socket_t sock2(ctx, zmq::socket_type::pair);
66+
sock1.bind("inproc://test");
67+
sock2.connect("inproc://test");
68+
69+
std::array<zmq::const_buffer, 2> send_msgs = {
70+
zmq::str_buffer("foo"),
71+
zmq::str_buffer("bar!")
72+
};
73+
if (!zmq::send_multipart(sock1, send_msgs))
74+
return 1;
75+
76+
std::vector<zmq::message_t> recv_msgs;
77+
const auto ret = zmq::recv_multipart(
78+
sock2, std::back_inserter(recv_msgs));
79+
if (!ret)
80+
return 1;
81+
std::cout << "Got " << *ret
82+
<< " messages" << std::endl;
83+
return 0;
84+
}
85+
```
5786

5887
Contribution policy
5988
===================

0 commit comments

Comments
 (0)