mirror of
https://github.com/boostorg/asio.git
synced 2026-01-28 06:42:08 +00:00
........ r43377 | chris_kohlhoff | 2008-02-23 09:43:54 +1100 (Sat, 23 Feb 2008) | 2 lines Use the correct vector of timer queues when dispatching timers. ........ r43437 | chris_kohlhoff | 2008-02-29 23:57:57 +1100 (Fri, 29 Feb 2008) | 2 lines Add missing tie(). ........ r43469 | chris_kohlhoff | 2008-03-04 00:21:05 +1100 (Tue, 04 Mar 2008) | 4 lines Disable use of CancelIo by default, due to the possibility of silent failure on some system configurations. Swallow error returned by CancelIoEx if there are no operations to be cancelled. ........ r43470 | chris_kohlhoff | 2008-03-04 00:27:06 +1100 (Tue, 04 Mar 2008) | 2 lines Add missing 'boost_' prefix to helper namespace. ........ r43471 | chris_kohlhoff | 2008-03-04 00:36:35 +1100 (Tue, 04 Mar 2008) | 2 lines Regenerate documentation. ........ r43472 | chris_kohlhoff | 2008-03-04 01:05:35 +1100 (Tue, 04 Mar 2008) | 1 line Update copyright notices. ........ r43473 | chris_kohlhoff | 2008-03-04 01:13:01 +1100 (Tue, 04 Mar 2008) | 2 lines Update copyright notices. ........ r43569 | chris_kohlhoff | 2008-03-13 00:25:49 +1100 (Thu, 13 Mar 2008) | 4 lines Revert to having the windows-bug workaround (short timeout on GetQueuedCompletionStatus) on all threads as there are still scenarios where threads can get stuck indefinitely. ........ [SVN r43571]
96 lines
2.7 KiB
C++
96 lines
2.7 KiB
C++
//
|
|
// daytime_client.cpp
|
|
// ~~~~~~~~~~~~~~~~~~
|
|
//
|
|
// Copyright (c) 2003-2008 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 <boost/asio.hpp>
|
|
#include <boost/bind.hpp>
|
|
#include <iostream>
|
|
#include "logger.hpp"
|
|
#include "stream_socket_service.hpp"
|
|
|
|
typedef boost::asio::basic_stream_socket<boost::asio::ip::tcp,
|
|
services::stream_socket_service<boost::asio::ip::tcp> > debug_stream_socket;
|
|
|
|
char read_buffer[1024];
|
|
|
|
void read_handler(const boost::system::error_code& e,
|
|
std::size_t bytes_transferred, debug_stream_socket* s)
|
|
{
|
|
if (!e)
|
|
{
|
|
std::cout.write(read_buffer, bytes_transferred);
|
|
|
|
s->async_read_some(boost::asio::buffer(read_buffer),
|
|
boost::bind(read_handler, boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred, s));
|
|
}
|
|
}
|
|
|
|
void connect_handler(const boost::system::error_code& e, debug_stream_socket* s,
|
|
boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
|
|
{
|
|
if (!e)
|
|
{
|
|
s->async_read_some(boost::asio::buffer(read_buffer),
|
|
boost::bind(read_handler, boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred, s));
|
|
}
|
|
else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
|
|
{
|
|
s->close();
|
|
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
|
|
s->async_connect(endpoint,
|
|
boost::bind(connect_handler,
|
|
boost::asio::placeholders::error, s, ++endpoint_iterator));
|
|
}
|
|
else
|
|
{
|
|
std::cerr << e.message() << std::endl;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
try
|
|
{
|
|
if (argc != 2)
|
|
{
|
|
std::cerr << "Usage: daytime_client <host>" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
boost::asio::io_service io_service;
|
|
|
|
// Set the name of the file that all logger instances will use.
|
|
services::logger logger(io_service, "");
|
|
logger.use_file("log.txt");
|
|
|
|
// Resolve the address corresponding to the given host.
|
|
boost::asio::ip::tcp::resolver resolver(io_service);
|
|
boost::asio::ip::tcp::resolver::query query(argv[1], "daytime");
|
|
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
|
|
boost::asio::ip::tcp::endpoint endpoint = *iterator;
|
|
|
|
// Start an asynchronous connect.
|
|
debug_stream_socket socket(io_service);
|
|
socket.async_connect(endpoint,
|
|
boost::bind(connect_handler,
|
|
boost::asio::placeholders::error, &socket, ++iterator));
|
|
|
|
// Run the io_service until all operations have finished.
|
|
io_service.run();
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|