src/examples/cpp03/echo/blocking_tcp_echo_client.cpp | src/examples/cpp11/echo/blocking_tcp_echo_client.cpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·blocking_tcp_echo_client.cpp | 2 | //·blocking_tcp_echo_client.cpp |
3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 3 | //·~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // | 4 | // |
5 | //·Copyright·(c)·2003-2018·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2018·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) |
6 | // | 6 | // |
7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 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) | 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::tcp; | 16 | using·asio::ip::tcp; |
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 | ··try | 22 | ··try |
23 | ··{ | 23 | ··{ |
24 | ····if·(argc·!=·3) | 24 | ····if·(argc·!=·3) |
25 | ····{ | 25 | ····{ |
26 | ······std::cerr·<<·"Usage:·blocking_tcp_echo_client·<host>·<port>\n"; | 26 | ······std::cerr·<<·"Usage:·blocking_tcp_echo_client·<host>·<port>\n"; |
27 | ······return·1; | 27 | ······return·1; |
28 | ····} | 28 | ····} |
29 | | 29 | |
30 | ····asio::io_context·io_context; | 30 | ····asio::io_context·io_context; |
31 | | 31 | |
32 | ····tcp::resolver·resolver(io_context); | |
33 | ····tcp::resolver::results_type·endpoints·= | |
34 | ······resolver.resolve(tcp::v4(),·argv[1],·argv[2]); | |
35 | | |
36 | ····tcp::socket·s(io_context); | 32 | ····tcp::socket·s(io_context); |
37 | ····asio::connect(s,·endpoints); | 33 | ····tcp::resolver·resolver(io_context); |
| 34 | ····asio::connect(s,·resolver.resolve(argv[1],·argv[2])); |
38 | | 35 | |
39 | ····using·namespace·std;·//·For·strlen. | |
40 | ····std::cout·<<·"Enter·message:·"; | 36 | ····std::cout·<<·"Enter·message:·"; |
41 | ····char·request[max_length]; | 37 | ····char·request[max_length]; |
42 | ····std::cin.getline(request,·max_length); | 38 | ····std::cin.getline(request,·max_length); |
43 | ····size_t·request_length·=·strlen(request); | 39 | ····size_t·request_length·=·std::strlen(request); |
44 | ····asio::write(s,·asio::buffer(request,·request_length)); | 40 | ····asio::write(s,·asio::buffer(request,·request_length)); |
45 | | 41 | |
46 | ····char·reply[max_length]; | 42 | ····char·reply[max_length]; |
47 | ····size_t·reply_length·=·asio::read(s, | 43 | ····size_t·reply_length·=·asio::read(s, |
48 | ········asio::buffer(reply,·request_length)); | 44 | ········asio::buffer(reply,·request_length)); |
49 | ····std::cout·<<·"Reply·is:·"; | 45 | ····std::cout·<<·"Reply·is:·"; |
50 | ····std::cout.write(reply,·reply_length); | 46 | ····std::cout.write(reply,·reply_length); |
51 | ····std::cout·<<·"\n"; | 47 | ····std::cout·<<·"\n"; |
52 | ··} | 48 | ··} |
53 | ··catch·(std::exception&·e) | 49 | ··catch·(std::exception&·e) |
54 | ··{ | 50 | ··{ |
55 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 51 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; |
56 | ··} | 52 | ··} |
57 | | 53 | |
58 | ··return·0; | 54 | ··return·0; |
59 | } | 55 | } |