]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/multicast/receiver.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / multicast / receiver.cpp
CommitLineData
7c673cae
FG
1//
2// receiver.cpp
3// ~~~~~~~~~~~~
4//
b32b8144 5// Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7c673cae
FG
6//
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)
9//
10
11#include <iostream>
12#include <string>
13#include <boost/asio.hpp>
14#include "boost/bind.hpp"
15
16const short multicast_port = 30001;
17
18class receiver
19{
20public:
b32b8144 21 receiver(boost::asio::io_context& io_context,
7c673cae
FG
22 const boost::asio::ip::address& listen_address,
23 const boost::asio::ip::address& multicast_address)
b32b8144 24 : socket_(io_context)
7c673cae
FG
25 {
26 // Create the socket so that multiple may be bound to the same address.
27 boost::asio::ip::udp::endpoint listen_endpoint(
28 listen_address, multicast_port);
29 socket_.open(listen_endpoint.protocol());
30 socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
31 socket_.bind(listen_endpoint);
32
33 // Join the multicast group.
34 socket_.set_option(
35 boost::asio::ip::multicast::join_group(multicast_address));
36
37 socket_.async_receive_from(
38 boost::asio::buffer(data_, max_length), sender_endpoint_,
39 boost::bind(&receiver::handle_receive_from, this,
40 boost::asio::placeholders::error,
41 boost::asio::placeholders::bytes_transferred));
42 }
43
44 void handle_receive_from(const boost::system::error_code& error,
45 size_t bytes_recvd)
46 {
47 if (!error)
48 {
49 std::cout.write(data_, bytes_recvd);
50 std::cout << std::endl;
51
52 socket_.async_receive_from(
53 boost::asio::buffer(data_, max_length), sender_endpoint_,
54 boost::bind(&receiver::handle_receive_from, this,
55 boost::asio::placeholders::error,
56 boost::asio::placeholders::bytes_transferred));
57 }
58 }
59
60private:
61 boost::asio::ip::udp::socket socket_;
62 boost::asio::ip::udp::endpoint sender_endpoint_;
63 enum { max_length = 1024 };
64 char data_[max_length];
65};
66
67int main(int argc, char* argv[])
68{
69 try
70 {
71 if (argc != 3)
72 {
73 std::cerr << "Usage: receiver <listen_address> <multicast_address>\n";
74 std::cerr << " For IPv4, try:\n";
75 std::cerr << " receiver 0.0.0.0 239.255.0.1\n";
76 std::cerr << " For IPv6, try:\n";
77 std::cerr << " receiver 0::0 ff31::8000:1234\n";
78 return 1;
79 }
80
b32b8144
FG
81 boost::asio::io_context io_context;
82 receiver r(io_context,
83 boost::asio::ip::make_address(argv[1]),
84 boost::asio::ip::make_address(argv[2]));
85 io_context.run();
7c673cae
FG
86 }
87 catch (std::exception& e)
88 {
89 std::cerr << "Exception: " << e.what() << "\n";
90 }
91
92 return 0;
93}