Skip to content

Commit 379793c

Browse files
committed
Compile and update the examples in the README
1 parent 7a6c904 commit 379793c

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ int main()
5353
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
5454
}
5555
```
56-
This a more complex example where we send and receive multi-part messages.
56+
This a more complex example where we send and receive multi-part messages over TCP with a wildcard port.
5757
```c++
5858
#include <iostream>
5959
#include <zmq_addon.hpp>
6060

6161
int main()
6262
{
6363
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");
64+
zmq::socket_t sock1(ctx, zmq::socket_type::push);
65+
zmq::socket_t sock2(ctx, zmq::socket_type::pull);
66+
sock1.bind("tcp://127.0.0.1:*");
67+
const std::string last_endpoint =
68+
sock1.get(zmq::sockopt::last_endpoint);
69+
std::cout << "Connecting to "
70+
<< last_endpoint << std::endl;
71+
sock2.connect(last_endpoint);
6872

6973
std::array<zmq::const_buffer, 2> send_msgs = {
7074
zmq::str_buffer("foo"),
@@ -84,6 +88,8 @@ int main()
8488
}
8589
```
8690

91+
See the `examples` directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable.
92+
8793
Compatibility Guidelines
8894
========================
8995

examples/CMakeLists.txt

+19-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ add_executable(
1515
pubsub_multithread_inproc
1616
pubsub_multithread_inproc.cpp
1717
)
18-
1918
target_link_libraries(
2019
pubsub_multithread_inproc
21-
PRIVATE cppzmq
22-
PRIVATE ${CMAKE_THREAD_LIBS_INIT}
20+
PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT}
21+
)
22+
23+
add_executable(
24+
hello_world
25+
hello_world.cpp
26+
)
27+
target_link_libraries(
28+
hello_world
29+
PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT}
30+
)
31+
32+
add_executable(
33+
multipart_messages
34+
multipart_messages.cpp
35+
)
36+
target_link_libraries(
37+
multipart_messages
38+
PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT}
2339
)

examples/hello_world.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <zmq.hpp>
2+
3+
int main()
4+
{
5+
zmq::context_t ctx;
6+
zmq::socket_t sock(ctx, zmq::socket_type::push);
7+
sock.bind("inproc://test");
8+
sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait);
9+
}

examples/multipart_messages.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <zmq_addon.hpp>
3+
4+
int main()
5+
{
6+
zmq::context_t ctx;
7+
zmq::socket_t sock1(ctx, zmq::socket_type::push);
8+
zmq::socket_t sock2(ctx, zmq::socket_type::pull);
9+
sock1.bind("tcp://127.0.0.1:*");
10+
const std::string last_endpoint =
11+
sock1.get(zmq::sockopt::last_endpoint);
12+
std::cout << "Connecting to "
13+
<< last_endpoint << std::endl;
14+
sock2.connect(last_endpoint);
15+
16+
std::array<zmq::const_buffer, 2> send_msgs = {
17+
zmq::str_buffer("foo"),
18+
zmq::str_buffer("bar!")
19+
};
20+
if (!zmq::send_multipart(sock1, send_msgs))
21+
return 1;
22+
23+
std::vector<zmq::message_t> recv_msgs;
24+
const auto ret = zmq::recv_multipart(
25+
sock2, std::back_inserter(recv_msgs));
26+
if (!ret)
27+
return 1;
28+
std::cout << "Got " << *ret
29+
<< " messages" << std::endl;
30+
return 0;
31+
}

0 commit comments

Comments
 (0)