src/​examples/​cpp03/​echo/​blocking_udp_echo_cli​ent.​cppsrc/​examples/​cpp11/​echo/​blocking_udp_echo_cli​ent.​cpp
1 /​/​1 /​/​
2 /​/​·​blocking_udp_echo_cli​ent.​cpp2 /​/​·​blocking_udp_echo_cli​ent.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~~~~~~​~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2015·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2015·​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·​<cstdlib>11 #include·​<cstdlib>
12 #include·​<cstring>12 #include·​<cstring>
13 #include·​<iostream>13 #include·​<iostream>
14 #include·​"asio.​hpp"14 #include·​"asio.​hpp"
15 15
16 using·​asio:​:​ip:​:​udp;​16 using·​asio:​:​ip:​:​udp;​
17 17
18 enum·​{·​max_length·​=·​1024·​};​18 enum·​{·​max_length·​=·​1024·​};​
19 19
20 int·​main(int·​argc,​·​char*·​argv[])​20 int·​main(int·​argc,​·​char*·​argv[])​
21 {21 {
22 ··​try22 ··​try
23 ··​{23 ··​{
24 ····​if·​(argc·​!=·​3)​24 ····​if·​(argc·​!=·​3)​
25 ····​{25 ····​{
26 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_udp_echo_cli​ent·​<host>·​<port>\n";​26 ······​std:​:​cerr·​<<·​"Usage:​·​blocking_udp_echo_cli​ent·​<host>·​<port>\n";​
27 ······​return·​1;​27 ······​return·​1;​
28 ····​}28 ····​}
29 29
30 ····​asio:​:​io_service·​io_service;​30 ····​asio:​:​io_service·​io_service;​
31 31
32 ····​udp:​:​socket·​s(io_service,​·​udp:​:​endpoint(udp:​:​v4()​,​·​0)​)​;​32 ····​udp:​:​socket·​s(io_service,​·​udp:​:​endpoint(udp:​:​v4()​,​·​0)​)​;​
33 33
34 ····​udp:​:​resolver·​resolver(io_service)​;​34 ····​udp:​:​resolver·​resolver(io_service)​;​
35 ····​udp:​:​resolver:​:​query·query(udp:​:​v4()​,​·​argv[1],​·​argv[2])​;​35 ····​udp:​:​endpoint·endpoint·=·*resolver.​resolve({udp:​:​v4()​,​·​argv[1],​·​argv[2]})​;​
36 ····udp:​:​resolver:​:​iterator·iterator·=·resolver.​resolve(query)​;​
37 36
38 ····​using·​namespace·​std;​·​/​/​·​For·​strlen.​
39 ····​std:​:​cout·​<<·​"Enter·​message:​·​";​37 ····​std:​:​cout·​<<·​"Enter·​message:​·​";​
40 ····​char·​request[max_length];​38 ····​char·​request[max_length];​
41 ····​std:​:​cin.​getline(request,​·​max_length)​;​39 ····​std:​:​cin.​getline(request,​·​max_length)​;​
42 ····​size_t·​request_length·​=·​strlen(request)​;​40 ····​size_t·​request_length·​=·std:​:​strlen(request)​;​
43 ····​s.​send_to(asio:​:​buffer(request,​·​request_length)​,​·*iterator)​;​41 ····​s.​send_to(asio:​:​buffer(request,​·​request_length)​,​·endpoint)​;​
44 42
45 ····​char·​reply[max_length];​43 ····​char·​reply[max_length];​
46 ····​udp:​:​endpoint·​sender_endpoint;​44 ····​udp:​:​endpoint·​sender_endpoint;​
47 ····​size_t·​reply_length·​=·​s.​receive_from(45 ····​size_t·​reply_length·​=·​s.​receive_from(
48 ········​asio:​:​buffer(reply,​·​max_length)​,​·​sender_endpoint)​;​46 ········​asio:​:​buffer(reply,​·​max_length)​,​·​sender_endpoint)​;​
49 ····​std:​:​cout·​<<·​"Reply·​is:​·​";​47 ····​std:​:​cout·​<<·​"Reply·​is:​·​";​
50 ····​std:​:​cout.​write(reply,​·​reply_length)​;​48 ····​std:​:​cout.​write(reply,​·​reply_length)​;​
51 ····​std:​:​cout·​<<·​"\n";​49 ····​std:​:​cout·​<<·​"\n";​
52 ··​}50 ··​}
53 ··​catch·​(std:​:​exception&·​e)​51 ··​catch·​(std:​:​exception&·​e)​
54 ··​{52 ··​{
55 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​53 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
56 ··​}54 ··​}
57 55
58 ··​return·​0;​56 ··​return·​0;​
59 }57 }