src/​examples/​cpp03/​local/​stream_client.​cppsrc/​examples/​cpp11/​local/​stream_client.​cpp
1 /​/​1 /​/​
2 /​/​·​stream_client.​cpp2 /​/​·​stream_client.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2023·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2023·​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 #if·​defined(ASIO_HAS_LOCA​L_SOCKETS)​16 #if·​defined(ASIO_HAS_LOCA​L_SOCKETS)​
17 17
18 using·​asio:​:​local:​:​stream_protocol;​18 using·​asio:​:​local:​:​stream_protocol;​
19 19
20 enum·{·​max_length·​=·​1024·};​20 constexpr·std:​:​size_t·​max_length·​=·​1024;​
21 21
22 int·​main(int·​argc,​·​char*·​argv[])​22 int·​main(int·​argc,​·​char*·​argv[])​
23 {23 {
24 ··​try24 ··​try
25 ··​{25 ··​{
26 ····​if·​(argc·​!=·​2)​26 ····​if·​(argc·​!=·​2)​
27 ····​{27 ····​{
28 ······​std:​:​cerr·​<<·​"Usage:​·​stream_client·​<file>\n";​28 ······​std:​:​cerr·​<<·​"Usage:​·​stream_client·​<file>\n";​
29 ······​return·​1;​29 ······​return·​1;​
30 ····​}30 ····​}
31 31
32 ····​asio:​:​io_context·​io_context;​32 ····​asio:​:​io_context·​io_context;​
33 33
34 ····​stream_protocol:​:​socket·​s(io_context)​;​34 ····​stream_protocol:​:​socket·​s(io_context)​;​
35 ····​s.​connect(stream_protoc​ol:​:​endpoint(argv[1])​)​;​35 ····​s.​connect(stream_protoc​ol:​:​endpoint(argv[1])​)​;​
36 36
37 ····​using·​namespace·​std;​·​/​/​·​For·​strlen.​
38 ····​std:​:​cout·​<<·​"Enter·​message:​·​";​37 ····​std:​:​cout·​<<·​"Enter·​message:​·​";​
39 ····​char·​request[max_length];​38 ····​char·​request[max_length];​
40 ····​std:​:​cin.​getline(request,​·​max_length)​;​39 ····​std:​:​cin.​getline(request,​·​max_length)​;​
41 ····​size_t·​request_length·​=·​strlen(request)​;​40 ····​size_t·​request_length·​=·std:​:​strlen(request)​;​
42 ····​asio:​:​write(s,​·​asio:​:​buffer(request,​·​request_length)​)​;​41 ····​asio:​:​write(s,​·​asio:​:​buffer(request,​·​request_length)​)​;​
43 42
44 ····​char·​reply[max_length];​43 ····​char·​reply[max_length];​
45 ····​size_t·​reply_length·​=·​asio:​:​read(s,​44 ····​size_t·​reply_length·​=·​asio:​:​read(s,​
46 ········​asio:​:​buffer(reply,​·​request_length)​)​;​45 ········​asio:​:​buffer(reply,​·​request_length)​)​;​
47 ····​std:​:​cout·​<<·​"Reply·​is:​·​";​46 ····​std:​:​cout·​<<·​"Reply·​is:​·​";​
48 ····​std:​:​cout.​write(reply,​·​reply_length)​;​47 ····​std:​:​cout.​write(reply,​·​reply_length)​;​
49 ····​std:​:​cout·​<<·​"\n";​48 ····​std:​:​cout·​<<·​"\n";​
50 ··​}49 ··​}
51 ··​catch·​(std:​:​exception&·​e)​50 ··​catch·​(std:​:​exception&·​e)​
52 ··​{51 ··​{
53 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​52 ····​std:​:​cerr·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
54 ··​}53 ··​}
55 54
56 ··​return·​0;​55 ··​return·​0;​
57 }56 }
58 57
59 #else·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​58 #else·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​
60 #·​error·​Local·​sockets·​not·​available·​on·​this·​platform.​59 #·​error·​Local·​sockets·​not·​available·​on·​this·​platform.​
61 #endif·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​60 #endif·​/​/​·​defined(ASIO_HAS_LOCA​L_SOCKETS)​