mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-03-04 07:27:27 +01:00
Add a more complex example involving multi-part messages
This commit is contained in:
parent
505edeb336
commit
c9225c17db
33
README.md
33
README.md
@ -41,9 +41,8 @@ Supported platforms
|
||||
|
||||
Examples
|
||||
========
|
||||
This example requires at least C++11.
|
||||
These examples requires at least C++11.
|
||||
```c++
|
||||
#include <string>
|
||||
#include <zmq.hpp>
|
||||
|
||||
int main()
|
||||
@ -54,6 +53,36 @@ int main()
|
||||
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
|
||||
}
|
||||
```
|
||||
This a more complex example where we send and receive multi-part messages.
|
||||
```c++
|
||||
#include <iostream>
|
||||
#include <zmq_addon.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t sock1(ctx, zmq::socket_type::pair);
|
||||
zmq::socket_t sock2(ctx, zmq::socket_type::pair);
|
||||
sock1.bind("inproc://test");
|
||||
sock2.connect("inproc://test");
|
||||
|
||||
std::array<zmq::const_buffer, 2> send_msgs = {
|
||||
zmq::str_buffer("foo"),
|
||||
zmq::str_buffer("bar!")
|
||||
};
|
||||
if (!zmq::send_multipart(sock1, send_msgs))
|
||||
return 1;
|
||||
|
||||
std::vector<zmq::message_t> recv_msgs;
|
||||
const auto ret = zmq::recv_multipart(
|
||||
sock2, std::back_inserter(recv_msgs));
|
||||
if (!ret)
|
||||
return 1;
|
||||
std::cout << "Got " << *ret
|
||||
<< " messages" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
Contribution policy
|
||||
===================
|
||||
|
Loading…
x
Reference in New Issue
Block a user