src/​examples/​cpp03/​multicast/​receiver.​cppsrc/​examples/​cpp11/​multicast/​receiver.​cpp
⋮⋮
1 /​/​1 /​/​
2 /​/​·​receiver.​cpp2 /​/​·​receiver.​cpp
3 /​/​·​~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​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)​8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 /​/​9 /​/​
10 10
11 #include·​<array>
11 #include·​<iostream>12 #include·​<iostream>
12 #include·​<string>13 #include·​<string>
13 #include·​"asio.​hpp"14 #include·​"asio.​hpp"
14 #include·​"boost/​bind/​bind.​hpp"
15 15
16 const·​short·​multicast_port·​=·​30001;​16 constexpr·​short·​multicast_port·​=·​30001;​
17 17
18 class·​receiver18 class·​receiver
19 {19 {
20 public:​20 public:​
21 ·​·​receiver(asio:​:​io_context&·​io_context,​21 ·​·​receiver(asio:​:​io_context&·​io_context,​
22 ·​·​·​·​·​·​const·​asio:​:​ip:​:​address&·​listen_address,​22 ·​·​·​·​·​·​const·​asio:​:​ip:​:​address&·​listen_address,​
23 ·​·​·​·​·​·​const·​asio:​:​ip:​:​address&·​multicast_address)​23 ·​·​·​·​·​·​const·​asio:​:​ip:​:​address&·​multicast_address)​
24 ·​·​·​·​:​·​socket_(io_context)​24 ·​·​·​·​:​·​socket_(io_context)​
25 ·​·​{25 ·​·​{
26 ·​·​·​·​/​/​·​Create·​the·​socket·​so·​that·​multiple·​may·​be·​bound·​to·​the·​same·​address.​26 ·​·​·​·​/​/​·​Create·​the·​socket·​so·​that·​multiple·​may·​be·​bound·​to·​the·​same·​address.​
27 ·​·​·​·​asio:​:​ip:​:​udp:​:​endpoint·​listen_endpoint(27 ·​·​·​·​asio:​:​ip:​:​udp:​:​endpoint·​listen_endpoint(
28 ·​·​·​·​·​·​·​·​listen_address,​·​multicast_port)​;​28 ·​·​·​·​·​·​·​·​listen_address,​·​multicast_port)​;​
29 ·​·​·​·​socket_.​open(listen_endpoint.​protocol()​)​;​29 ·​·​·​·​socket_.​open(listen_endpoint.​protocol()​)​;​
30 ·​·​·​·​socket_.​set_option(asio:​:​ip:​:​udp:​:​socket:​:​reuse_address(true)​)​;​30 ·​·​·​·​socket_.​set_option(asio:​:​ip:​:​udp:​:​socket:​:​reuse_address(true)​)​;​
31 ·​·​·​·​socket_.​bind(listen_endpoint)​;​31 ·​·​·​·​socket_.​bind(listen_endpoint)​;​
32 32
33 ·​·​·​·​/​/​·​Join·​the·​multicast·​group.​33 ·​·​·​·​/​/​·​Join·​the·​multicast·​group.​
34 ·​·​·​·​socket_.​set_option(34 ·​·​·​·​socket_.​set_option(
35 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​multicast:​:​join_group(multicast_​address)​)​;​35 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​multicast:​:​join_group(multicast_​address)​)​;​
36 36
37 ·​·​·​·​socket_.​async_receive_from(37 ·​·​·​·​do_receive()​;​
38 ·​·​·​·​·​·​·​·​asio:​:​buffer(data_,​·​max_length)​,​·​sender_endpoint_,​
39 ·​·​·​·​·​·​·​·​boost:​:​bind(&receiver:​:​handle_receive_from,​·​this,​
40 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error,​
41 ·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​bytes_transferred)​)​;​
42 ·​·​}38 ·​·​}
43 39
44 ·​·​void·​handle_receive_from(c​onst·​asio:​:​error_code&·​error,​40 private:​
45 ·​·​·​·​·​·​size_t·​bytes_recvd)​41 ·​·​void·​do_receive()​
46 ·​·​{42 ·​·​{
47 ·​·​·​·​if·​(!error)​43 ·​·​·​·​socket_.​async_receive_from(
48 ·​·​·​·​{44 ·​·​·​·​·​·​·​·​asio:​:​buffer(data_)​,​·​sender_endpoint_,​
49 ·​·​·​·​·​·​std:​:​cout.​write(data_,​·​bytes_recvd)​;​45 ·​·​·​·​·​·​·​·​[this](std:​:​error_code·​ec,​·​std:​:​size_t·​length)​
50 ·​·​·​·​·​·​std:​:​cout·​<<·​std:​:​endl;​46 ·​·​·​·​·​·​·​·​{
51 47 ·​·​·​·​·​·​·​·​·​·​if·​(!ec)​
52 ·​·​·​·​·​·​socket_.​async_receive_from(48 ·​·​·​·​·​·​·​·​·​·​{
53 ·​·​·​·​·​·​·​·​·​·​asio:​:​buffer(data_,​·​max_length)​,​·​sender_endpoint_,​49 ·​·​·​·​·​·​·​·​·​·​·​·​std:​:​cout.​write(data_.​data()​,​·​length)​;​
54 ·​·​·​·​·​·​·​·​·​·​boost:​:​bind(&receiver:​:​handle_receive_from,​·​this,​50 ·​·​·​·​·​·​·​·​·​·​·​·​std:​:​cout·​<<·​std:​:​endl;​
55 ·​·​·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​error,​51
56 ·​·​·​·​·​·​·​·​·​·​·​·​asio:​:​placeholders:​:​bytes_transferred)​)​;​52 ·​·​·​·​·​·​·​·​·​·​·​·​do_receive()​;​
57 ·​·​·​·​}53 ·​·​·​·​·​·​·​·​·​·​}
54 ·​·​·​·​·​·​·​·​})​;​
58 ·​·​}55 ·​·​}
59 56
60 private:​
61 ·​·​asio:​:​ip:​:​udp:​:​socket·​socket_;​57 ·​·​asio:​:​ip:​:​udp:​:​socket·​socket_;​
62 ·​·​asio:​:​ip:​:​udp:​:​endpoint·​sender_endpoint_;​58 ·​·​asio:​:​ip:​:​udp:​:​endpoint·​sender_endpoint_;​
63 ·​·​enum·​{·​max_length·​=·​1024·​};​59 ·​·​std:​:​array<char,​·​1024>·​data_;​
64 ·​·​char·​data_[max_length];​
65 };​60 };​
66 61
67 int·​main(int·​argc,​·​char*·​argv[])​62 int·​main(int·​argc,​·​char*·​argv[])​
68 {63 {
69 ·​·​try64 ·​·​try
70 ·​·​{65 ·​·​{
71 ·​·​·​·​if·​(argc·​!=·​3)​66 ·​·​·​·​if·​(argc·​!=·​3)​
72 ·​·​·​·​{67 ·​·​·​·​{
73 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​receiver·​<listen_address>·​<multicast_address>\n​";​68 ·​·​·​·​·​·​std:​:​cerr·​<<·​"Usage:​·​receiver·​<listen_address>·​<multicast_address>\n​";​
74 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​For·​IPv4,​·​try:​\n";​69 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​For·​IPv4,​·​try:​\n";​
75 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​·​·​receiver·​0.​0.​0.​0·​239.​255.​0.​1\n";​70 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​·​·​receiver·​0.​0.​0.​0·​239.​255.​0.​1\n";​
76 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​For·​IPv6,​·​try:​\n";​71 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​For·​IPv6,​·​try:​\n";​
77 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​·​·​receiver·​0:​:​0·​ff31:​:​8000:​1234\n";​72 ·​·​·​·​·​·​std:​:​cerr·​<<·​"·​·​·​·​receiver·​0:​:​0·​ff31:​:​8000:​1234\n";​
78 ·​·​·​·​·​·​return·​1;​73 ·​·​·​·​·​·​return·​1;​
79 ·​·​·​·​}74 ·​·​·​·​}
80 75
81 ·​·​·​·​asio:​:​io_context·​io_context;​76 ·​·​·​·​asio:​:​io_context·​io_context;​
82 ·​·​·​·​receiver·​r(io_context,​77 ·​·​·​·​receiver·​r(io_context,​
83 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​make_address(argv[1])​,​78 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​make_address(argv[1])​,​
84 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​make_address(argv[2])​)​;​79 ·​·​·​·​·​·​·​·​asio:​:​ip:​:​make_address(argv[2])​)​;​
85 ·​·​·​·​io_context.​run()​;​80 ·​·​·​·​io_context.​run()​;​
86 ·​·​}81 ·​·​}
87 ·​·​catch·​(std:​:​exception&·​e)​82 ·​·​catch·​(std:​:​exception&·​e)​
88 ·​·​{83 ·​·​{
89 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​84 ·​·​·​·​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
90 ·​·​}85 ·​·​}
91 86
92 ·​·​return·​0;​87 ·​·​return·​0;​
93 }88 }