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