Skip to content

Commit

Permalink
Compile and update the examples in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
gummif committed Oct 10, 2020
1 parent 7a6c904 commit 379793c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ 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 <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");
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<zmq::const_buffer, 2> send_msgs = {
zmq::str_buffer("foo"),
Expand All @@ -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
========================

Expand Down
22 changes: 19 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
9 changes: 9 additions & 0 deletions examples/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <zmq.hpp>

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);
}
31 changes: 31 additions & 0 deletions examples/multipart_messages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <zmq_addon.hpp>

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<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;
}

0 comments on commit 379793c

Please sign in to comment.