From 379793cbabe0afef7d5f1e1d481c54a43495897f Mon Sep 17 00:00:00 2001 From: Gudmundur Adalsteinsson Date: Sat, 10 Oct 2020 13:39:42 +0000 Subject: [PATCH] Compile and update the examples in the README --- README.md | 16 +++++++++++----- examples/CMakeLists.txt | 22 +++++++++++++++++++--- examples/hello_world.cpp | 9 +++++++++ examples/multipart_messages.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 examples/hello_world.cpp create mode 100644 examples/multipart_messages.cpp diff --git a/README.md b/README.md index 224a35da..8fe13e6b 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ 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. +This a more complex example where we send and receive multi-part messages over TCP with a wildcard port. ```c++ #include #include @@ -61,10 +61,14 @@ This a more complex example where we send and receive multi-part messages. 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"); + zmq::socket_t sock1(ctx, zmq::socket_type::push); + zmq::socket_t sock2(ctx, zmq::socket_type::pull); + sock1.bind("tcp://127.0.0.1:*"); + const std::string last_endpoint = + sock1.get(zmq::sockopt::last_endpoint); + std::cout << "Connecting to " + << last_endpoint << std::endl; + sock2.connect(last_endpoint); std::array send_msgs = { zmq::str_buffer("foo"), @@ -84,6 +88,8 @@ int main() } ``` +See the `examples` directory for more examples. When the project is compiled with tests enabled, each example gets compiled to an executable. + Compatibility Guidelines ======================== diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 491f3d15..2da867f5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -15,9 +15,25 @@ add_executable( pubsub_multithread_inproc pubsub_multithread_inproc.cpp ) - target_link_libraries( pubsub_multithread_inproc - PRIVATE cppzmq - PRIVATE ${CMAKE_THREAD_LIBS_INIT} + PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT} +) + +add_executable( + hello_world + hello_world.cpp +) +target_link_libraries( + hello_world + PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT} +) + +add_executable( + multipart_messages + multipart_messages.cpp +) +target_link_libraries( + multipart_messages + PRIVATE cppzmq ${CMAKE_THREAD_LIBS_INIT} ) diff --git a/examples/hello_world.cpp b/examples/hello_world.cpp new file mode 100644 index 00000000..87194482 --- /dev/null +++ b/examples/hello_world.cpp @@ -0,0 +1,9 @@ +#include + +int main() +{ + zmq::context_t ctx; + zmq::socket_t sock(ctx, zmq::socket_type::push); + sock.bind("inproc://test"); + sock.send(zmq::str_buffer("Hello, world"), zmq::send_flags::dontwait); +} diff --git a/examples/multipart_messages.cpp b/examples/multipart_messages.cpp new file mode 100644 index 00000000..1af8f8f0 --- /dev/null +++ b/examples/multipart_messages.cpp @@ -0,0 +1,31 @@ +#include +#include + +int main() +{ + zmq::context_t ctx; + zmq::socket_t sock1(ctx, zmq::socket_type::push); + zmq::socket_t sock2(ctx, zmq::socket_type::pull); + sock1.bind("tcp://127.0.0.1:*"); + const std::string last_endpoint = + sock1.get(zmq::sockopt::last_endpoint); + std::cout << "Connecting to " + << last_endpoint << std::endl; + sock2.connect(last_endpoint); + + std::array send_msgs = { + zmq::str_buffer("foo"), + zmq::str_buffer("bar!") + }; + if (!zmq::send_multipart(sock1, send_msgs)) + return 1; + + std::vector 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; +}