File tree Expand file tree Collapse file tree 4 files changed +70
-8
lines changed Expand file tree Collapse file tree 4 files changed +70
-8
lines changed Original file line number Diff line number Diff 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
6161int 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+
8793Compatibility Guidelines
8894========================
8995
Original file line number Diff line number Diff line change @@ -15,9 +15,25 @@ add_executable(
1515 pubsub_multithread_inproc
1616 pubsub_multithread_inproc.cpp
1717)
18-
1918target_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)
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments