~ubuntu-branches/ubuntu/intrepid/asio/intrepid

« back to all changes in this revision

Viewing changes to src/examples/timeouts/stream_receive_timeout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Simon Richter
  • Date: 2007-09-07 11:10:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070907111041-f0uwhs0llvzj9ah5
Tags: upstream-0.3.8~rc3
ImportĀ upstreamĀ versionĀ 0.3.8~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// stream_receive_timeout.cpp
 
3
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
 
4
//
 
5
// Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
 
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 "asio.hpp"
 
12
#include <boost/bind.hpp>
 
13
#include <boost/date_time/posix_time/posix_time_types.hpp>
 
14
#include <iostream>
 
15
 
 
16
using namespace asio;
 
17
using asio::ip::tcp;
 
18
 
 
19
class stream_handler
 
20
{
 
21
public:
 
22
  stream_handler(io_service& ios)
 
23
    : io_service_(ios),
 
24
      timer_(ios),
 
25
      acceptor_(ios, tcp::endpoint(tcp::v4(), 32123)),
 
26
      socket_(ios)
 
27
  {
 
28
    acceptor_.async_accept(socket_,
 
29
        boost::bind(&stream_handler::handle_accept, this,
 
30
          asio::placeholders::error));
 
31
  }
 
32
 
 
33
  void handle_accept(const asio::error_code& err)
 
34
  {
 
35
    if (err)
 
36
    {
 
37
      std::cout << "Accept error: " << err.message() << "\n";
 
38
    }
 
39
    else
 
40
    {
 
41
      std::cout << "Successful accept\n";
 
42
 
 
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));
 
48
    }
 
49
  }
 
50
 
 
51
  void handle_recv(const asio::error_code& err)
 
52
  {
 
53
    if (err)
 
54
    {
 
55
      std::cout << "Receive error: " << err.message() << "\n";
 
56
    }
 
57
    else
 
58
    {
 
59
      std::cout << "Successful receive\n";
 
60
    }
 
61
  }
 
62
 
 
63
  void close()
 
64
  {
 
65
    socket_.close();
 
66
  }
 
67
 
 
68
private:
 
69
  io_service& io_service_;
 
70
  deadline_timer timer_;
 
71
  tcp::acceptor acceptor_;
 
72
  tcp::socket socket_;
 
73
  char buf_[1024];
 
74
};
 
75
 
 
76
void connect_handler()
 
77
{
 
78
  std::cout << "Successful connect\n";
 
79
}
 
80
 
 
81
int main()
 
82
{
 
83
  try
 
84
  {
 
85
    io_service ios;
 
86
 
 
87
    stream_handler sh(ios);
 
88
 
 
89
    tcp::socket s(ios);
 
90
    s.async_connect(
 
91
        tcp::endpoint(asio::ip::address_v4::loopback(), 32123),
 
92
        boost::bind(connect_handler));
 
93
 
 
94
    ios.run();
 
95
  }
 
96
  catch (std::exception& e)
 
97
  {
 
98
    std::cerr << "Exception: " << e.what() << "\n";
 
99
  }
 
100
 
 
101
  return 0;
 
102
}