summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp b/3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp
new file mode 100644
index 00000000000..e0a6af9df6f
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp03/services/daytime_client.cpp
@@ -0,0 +1,97 @@
+//
+// daytime_client.cpp
+// ~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <asio.hpp>
+#include <boost/bind.hpp>
+#include <iostream>
+#include "logger.hpp"
+
+using asio::ip::tcp;
+
+char read_buffer[1024];
+
+void read_handler(const asio::error_code& e,
+ std::size_t bytes_transferred, tcp::socket* s)
+{
+ if (!e)
+ {
+ std::cout.write(read_buffer, bytes_transferred);
+
+ s->async_read_some(asio::buffer(read_buffer),
+ boost::bind(read_handler, asio::placeholders::error,
+ asio::placeholders::bytes_transferred, s));
+ }
+ else
+ {
+ services::logger logger(s->get_executor().context(), "read_handler");
+
+ std::string msg = "Read error: ";
+ msg += e.message();
+ logger.log(msg);
+ }
+}
+
+void connect_handler(const asio::error_code& e, tcp::socket* s)
+{
+ services::logger logger(s->get_executor().context(), "connect_handler");
+
+ if (!e)
+ {
+ logger.log("Connection established");
+
+ s->async_read_some(asio::buffer(read_buffer),
+ boost::bind(read_handler, asio::placeholders::error,
+ asio::placeholders::bytes_transferred, s));
+ }
+ else
+ {
+ std::string msg = "Unable to establish connection: ";
+ msg += e.message();
+ logger.log(msg);
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc != 2)
+ {
+ std::cerr << "Usage: daytime_client <host>" << std::endl;
+ return 1;
+ }
+
+ asio::io_context io_context;
+
+ // Set the name of the file that all logger instances will use.
+ services::logger logger(io_context, "");
+ logger.use_file("log.txt");
+
+ // Resolve the address corresponding to the given host.
+ tcp::resolver resolver(io_context);
+ tcp::resolver::results_type endpoints =
+ resolver.resolve(argv[1], "daytime");
+
+ // Start an asynchronous connect.
+ tcp::socket socket(io_context);
+ asio::async_connect(socket, endpoints,
+ boost::bind(connect_handler,
+ asio::placeholders::error, &socket));
+
+ // Run the io_context until all operations have finished.
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}