]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // daytime_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 <boost/asio.hpp> | |
12 | #include <boost/bind.hpp> | |
13 | #include <iostream> | |
14 | #include "logger.hpp" | |
7c673cae | 15 | |
b32b8144 | 16 | using boost::asio::ip::tcp; |
7c673cae FG |
17 | |
18 | char read_buffer[1024]; | |
19 | ||
20 | void read_handler(const boost::system::error_code& e, | |
b32b8144 | 21 | std::size_t bytes_transferred, tcp::socket* s) |
7c673cae FG |
22 | { |
23 | if (!e) | |
24 | { | |
25 | std::cout.write(read_buffer, bytes_transferred); | |
26 | ||
27 | s->async_read_some(boost::asio::buffer(read_buffer), | |
28 | boost::bind(read_handler, boost::asio::placeholders::error, | |
29 | boost::asio::placeholders::bytes_transferred, s)); | |
30 | } | |
b32b8144 FG |
31 | else |
32 | { | |
33 | services::logger logger(s->get_executor().context(), "read_handler"); | |
34 | ||
35 | std::string msg = "Read error: "; | |
36 | msg += e.message(); | |
37 | logger.log(msg); | |
38 | } | |
7c673cae FG |
39 | } |
40 | ||
b32b8144 | 41 | void connect_handler(const boost::system::error_code& e, tcp::socket* s) |
7c673cae | 42 | { |
b32b8144 FG |
43 | services::logger logger(s->get_executor().context(), "connect_handler"); |
44 | ||
7c673cae FG |
45 | if (!e) |
46 | { | |
b32b8144 FG |
47 | logger.log("Connection established"); |
48 | ||
7c673cae FG |
49 | s->async_read_some(boost::asio::buffer(read_buffer), |
50 | boost::bind(read_handler, boost::asio::placeholders::error, | |
51 | boost::asio::placeholders::bytes_transferred, s)); | |
52 | } | |
53 | else | |
54 | { | |
b32b8144 FG |
55 | std::string msg = "Unable to establish connection: "; |
56 | msg += e.message(); | |
57 | logger.log(msg); | |
7c673cae FG |
58 | } |
59 | } | |
60 | ||
61 | int main(int argc, char* argv[]) | |
62 | { | |
63 | try | |
64 | { | |
65 | if (argc != 2) | |
66 | { | |
67 | std::cerr << "Usage: daytime_client <host>" << std::endl; | |
68 | return 1; | |
69 | } | |
70 | ||
b32b8144 | 71 | boost::asio::io_context io_context; |
7c673cae FG |
72 | |
73 | // Set the name of the file that all logger instances will use. | |
b32b8144 | 74 | services::logger logger(io_context, ""); |
7c673cae FG |
75 | logger.use_file("log.txt"); |
76 | ||
77 | // Resolve the address corresponding to the given host. | |
b32b8144 FG |
78 | tcp::resolver resolver(io_context); |
79 | tcp::resolver::results_type endpoints = | |
80 | resolver.resolve(argv[1], "daytime"); | |
7c673cae FG |
81 | |
82 | // Start an asynchronous connect. | |
b32b8144 FG |
83 | tcp::socket socket(io_context); |
84 | boost::asio::async_connect(socket, endpoints, | |
7c673cae FG |
85 | boost::bind(connect_handler, |
86 | boost::asio::placeholders::error, &socket)); | |
87 | ||
b32b8144 FG |
88 | // Run the io_context until all operations have finished. |
89 | io_context.run(); | |
7c673cae FG |
90 | } |
91 | catch (std::exception& e) | |
92 | { | |
93 | std::cerr << e.what() << std::endl; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } |