1 //
2 // server.cpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 <ctime>
12 #include <iostream>
13 #include <string>
14 #include <boost/bind.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <boost/enable_shared_from_this.hpp>
17 #include <asio.hpp>
18 
19 using asio::ip::tcp;
20 
make_daytime_string()21 std::string make_daytime_string()
22 {
23   using namespace std; // For time_t, time and ctime;
24   time_t now = time(0);
25   return ctime(&now);
26 }
27 
28 class tcp_connection
29   : public boost::enable_shared_from_this<tcp_connection>
30 {
31 public:
32   typedef boost::shared_ptr<tcp_connection> pointer;
33 
create(asio::io_context & io_context)34   static pointer create(asio::io_context& io_context)
35   {
36     return pointer(new tcp_connection(io_context));
37   }
38 
socket()39   tcp::socket& socket()
40   {
41     return socket_;
42   }
43 
start()44   void start()
45   {
46     message_ = make_daytime_string();
47 
48     asio::async_write(socket_, asio::buffer(message_),
49         boost::bind(&tcp_connection::handle_write, shared_from_this(),
50           asio::placeholders::error,
51           asio::placeholders::bytes_transferred));
52   }
53 
54 private:
tcp_connection(asio::io_context & io_context)55   tcp_connection(asio::io_context& io_context)
56     : socket_(io_context)
57   {
58   }
59 
handle_write(const asio::error_code &,size_t)60   void handle_write(const asio::error_code& /*error*/,
61       size_t /*bytes_transferred*/)
62   {
63   }
64 
65   tcp::socket socket_;
66   std::string message_;
67 };
68 
69 class tcp_server
70 {
71 public:
tcp_server(asio::io_context & io_context)72   tcp_server(asio::io_context& io_context)
73     : acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
74   {
75     start_accept();
76   }
77 
78 private:
start_accept()79   void start_accept()
80   {
81     tcp_connection::pointer new_connection =
82       tcp_connection::create(acceptor_.get_executor().context());
83 
84     acceptor_.async_accept(new_connection->socket(),
85         boost::bind(&tcp_server::handle_accept, this, new_connection,
86           asio::placeholders::error));
87   }
88 
handle_accept(tcp_connection::pointer new_connection,const asio::error_code & error)89   void handle_accept(tcp_connection::pointer new_connection,
90       const asio::error_code& error)
91   {
92     if (!error)
93     {
94       new_connection->start();
95     }
96 
97     start_accept();
98   }
99 
100   tcp::acceptor acceptor_;
101 };
102 
main()103 int main()
104 {
105   try
106   {
107     asio::io_context io_context;
108     tcp_server server(io_context);
109     io_context.run();
110   }
111   catch (std::exception& e)
112   {
113     std::cerr << e.what() << std::endl;
114   }
115 
116   return 0;
117 }
118