2
// stream_receive_timeout.cpp
3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
12
#include <boost/bind.hpp>
13
#include <boost/date_time/posix_time/posix_time_types.hpp>
22
stream_handler(io_service& ios)
25
acceptor_(ios, tcp::endpoint(tcp::v4(), 32123)),
28
acceptor_.async_accept(socket_,
29
boost::bind(&stream_handler::handle_accept, this,
30
asio::placeholders::error));
33
void handle_accept(const asio::error_code& err)
37
std::cout << "Accept error: " << err.message() << "\n";
41
std::cout << "Successful accept\n";
43
socket_.async_read_some(asio::buffer(buf_, sizeof(buf_)),
44
boost::bind(&stream_handler::handle_recv, this,
45
asio::placeholders::error));
46
timer_.expires_from_now(boost::posix_time::seconds(5));
47
timer_.async_wait(boost::bind(&stream_handler::close, this));
51
void handle_recv(const asio::error_code& err)
55
std::cout << "Receive error: " << err.message() << "\n";
59
std::cout << "Successful receive\n";
69
io_service& io_service_;
70
deadline_timer timer_;
71
tcp::acceptor acceptor_;
76
void connect_handler()
78
std::cout << "Successful connect\n";
87
stream_handler sh(ios);
91
tcp::endpoint(asio::ip::address_v4::loopback(), 32123),
92
boost::bind(connect_handler));
96
catch (std::exception& e)
98
std::cerr << "Exception: " << e.what() << "\n";