src/examples/cpp03/local/iostream_client.cpp | src/examples/cpp11/local/iostream_client.cpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·stream_client.cpp | 2 | //·stream_client.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·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·<cstring> | 11 | #include·<cstring> |
12 | #include·<iostream> | 12 | #include·<iostream> |
13 | #include·"asio.hpp" | 13 | #include·"asio.hpp" |
14 | | 14 | |
15 | #if·defined(ASIO_HAS_LOCAL_SOCKETS) | 15 | #if·defined(ASIO_HAS_LOCAL_SOCKETS) |
16 | | 16 | |
17 | using·asio::local::stream_protocol; | 17 | using·asio::local::stream_protocol; |
18 | | 18 | |
19 | enum·{·max_length·=·1024·}; | 19 | constexpr·std::size_t·max_length·=·1024; |
20 | | 20 | |
21 | int·main(int·argc,·char*·argv[]) | 21 | int·main(int·argc,·char*·argv[]) |
22 | { | 22 | { |
23 | ··try | 23 | ··try |
24 | ··{ | 24 | ··{ |
25 | ····if·(argc·!=·2) | 25 | ····if·(argc·!=·2) |
26 | ····{ | 26 | ····{ |
27 | ······std::cerr·<<·"Usage:·iostream_client·<file>\n"; | 27 | ······std::cerr·<<·"Usage:·iostream_client·<file>\n"; |
28 | ······return·1; | 28 | ······return·1; |
29 | ····} | 29 | ····} |
30 | | 30 | |
31 | ····stream_protocol::endpoint·ep(argv[1]); | 31 | ····stream_protocol::endpoint·ep(argv[1]); |
32 | ····stream_protocol::iostream·s(ep); | 32 | ····stream_protocol::iostream·s(ep); |
33 | ····if·(!s) | 33 | ····if·(!s) |
34 | ····{ | 34 | ····{ |
35 | ······std::cerr·<<·"Unable·to·connect:·"·<<·s.error().message()·<<·std::endl; | 35 | ······std::cerr·<<·"Unable·to·connect:·"·<<·s.error().message()·<<·std::endl; |
36 | ······return·1; | 36 | ······return·1; |
37 | ····} | 37 | ····} |
38 | | 38 | |
39 | ····using·namespace·std;·//·For·strlen. | |
40 | ····std::cout·<<·"Enter·message:·"; | 39 | ····std::cout·<<·"Enter·message:·"; |
41 | ····char·request[max_length]; | 40 | ····char·request[max_length]; |
42 | ····std::cin.getline(request,·max_length); | 41 | ····std::cin.getline(request,·max_length); |
43 | ····size_t·length·=·strlen(request); | 42 | ····size_t·length·=·std::strlen(request); |
44 | ····s·<<·request; | 43 | ····s·<<·request; |
45 | | 44 | |
46 | ····char·reply[max_length]; | 45 | ····char·reply[max_length]; |
47 | ····s.read(reply,·length); | 46 | ····s.read(reply,·length); |
48 | ····std::cout·<<·"Reply·is:·"; | 47 | ····std::cout·<<·"Reply·is:·"; |
49 | ····std::cout.write(reply,·length); | 48 | ····std::cout.write(reply,·length); |
50 | ····std::cout·<<·"\n"; | 49 | ····std::cout·<<·"\n"; |
51 | ··} | 50 | ··} |
52 | ··catch·(std::exception&·e) | 51 | ··catch·(std::exception&·e) |
53 | ··{ | 52 | ··{ |
54 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; | 53 | ····std::cerr·<<·"Exception:·"·<<·e.what()·<<·"\n"; |
55 | ··} | 54 | ··} |
56 | | 55 | |
57 | ··return·0; | 56 | ··return·0; |
58 | } | 57 | } |
59 | | 58 | |
60 | #else·//·defined(ASIO_HAS_LOCAL_SOCKETS) | 59 | #else·//·defined(ASIO_HAS_LOCAL_SOCKETS) |
61 | #·error·Local·sockets·not·available·on·this·platform. | 60 | #·error·Local·sockets·not·available·on·this·platform. |
62 | #endif·//·defined(ASIO_HAS_LOCAL_SOCKETS) | 61 | #endif·//·defined(ASIO_HAS_LOCAL_SOCKETS) |