From fd654ad5b21f9fcdb320ce85675c6459cf3403cb Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Wed, 6 Aug 2014 06:21:18 +0000 Subject: [PATCH] x git-svn-id: svn://elaine/murooma/trunk@144 d8a302eb-03bc-478d-80e4-98257eca68ef --- blocking_tcp_echo_client.cpp | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 blocking_tcp_echo_client.cpp diff --git a/blocking_tcp_echo_client.cpp b/blocking_tcp_echo_client.cpp new file mode 100644 index 00000000..aa9acbf4 --- /dev/null +++ b/blocking_tcp_echo_client.cpp @@ -0,0 +1,88 @@ +// +// blocking_tcp_echo_client.cpp +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2010 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 +#include +#include +#include +#include +#include // localtime +#include // stringstream +#include + + +using boost::asio::ip::tcp; +using namespace std; +using namespace std::chrono; + + +enum { max_length = 1024 }; + + +std::string return_current_time_and_date() +{ + auto now = system_clock::now(); + auto in_time_t = system_clock::to_time_t(now); + system_clock::duration ms = now.time_since_epoch(); + char buff[20]; + strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&in_time_t)); + stringstream ss; + ss << buff << "." << std::setw(3) << std::setfill('0') << ((ms / milliseconds(1)) % 1000); + return ss.str(); +} + +int main(int argc, char* argv[]) +{ + if (argc != 3) + { + std::cerr << "Usage: blocking_tcp_echo_client \n"; + return 1; + } + try + { + boost::asio::io_service io_service; + tcp::resolver resolver(io_service); + tcp::resolver::query query(tcp::v4(), argv[1], argv[2]); + tcp::resolver::iterator iterator = resolver.resolve(query); + + while (true) + { + try + { + tcp::socket s(io_service); + s.connect(*iterator); + boost::array buf; + boost::system::error_code error; + + while (true) + { + size_t len = s.read_some(boost::asio::buffer(buf), error); + if (error == boost::asio::error::eof) + break; + + std::cout.write(buf.data(), len); + } + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + usleep(100*1000); + } + } + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} + +