]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/multicast/sender.cpp
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14 #include <boost/asio.hpp>
16 constexpr short multicast_port
= 30001;
17 constexpr int max_message_count
= 10;
22 sender(boost::asio::io_context
& io_context
,
23 const boost::asio::ip::address
& multicast_address
)
24 : endpoint_(multicast_address
, multicast_port
),
25 socket_(io_context
, endpoint_
.protocol()),
35 std::ostringstream os
;
36 os
<< "Message " << message_count_
++;
39 socket_
.async_send_to(
40 boost::asio::buffer(message_
), endpoint_
,
41 [this](boost::system::error_code ec
, std::size_t /*length*/)
43 if (!ec
&& message_count_
< max_message_count
)
50 timer_
.expires_after(std::chrono::seconds(1));
52 [this](boost::system::error_code ec
)
60 boost::asio::ip::udp::endpoint endpoint_
;
61 boost::asio::ip::udp::socket socket_
;
62 boost::asio::steady_timer timer_
;
67 int main(int argc
, char* argv
[])
73 std::cerr
<< "Usage: sender <multicast_address>\n";
74 std::cerr
<< " For IPv4, try:\n";
75 std::cerr
<< " sender 239.255.0.1\n";
76 std::cerr
<< " For IPv6, try:\n";
77 std::cerr
<< " sender ff31::8000:1234\n";
81 boost::asio::io_context io_context
;
82 sender
s(io_context
, boost::asio::ip::make_address(argv
[1]));
85 catch (std::exception
& e
)
87 std::cerr
<< "Exception: " << e
.what() << "\n";