mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-12 10:33:52 +01:00
Compile and update the examples in the README
This commit is contained in:
parent
7a6c904f94
commit
379793cbab
16
README.md
16
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 <iostream>
|
||||
#include <zmq_addon.hpp>
|
||||
@ -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<zmq::const_buffer, 2> 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
|
||||
========================
|
||||
|
||||
|
@ -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
examples/hello_world.cpp
Normal file
9
examples/hello_world.cpp
Normal file
@ -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
examples/multipart_messages.cpp
Normal file
31
examples/multipart_messages.cpp
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user