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