• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // daytime_server.cpp
3 // ~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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/asio.hpp>
15 
16 using boost::asio::ip::tcp;
17 
make_daytime_string()18 std::string make_daytime_string()
19 {
20   using namespace std; // For time_t, time and ctime;
21   time_t now = time(0);
22   return ctime(&now);
23 }
24 
main()25 int main()
26 {
27   try
28   {
29     boost::asio::io_context io_context;
30 
31     tcp::endpoint endpoint(tcp::v4(), 13);
32     tcp::acceptor acceptor(io_context, endpoint);
33 
34     for (;;)
35     {
36       tcp::iostream stream;
37       boost::system::error_code ec;
38       acceptor.accept(stream.socket(), ec);
39       if (!ec)
40       {
41         stream << make_daytime_string();
42       }
43     }
44   }
45   catch (std::exception& e)
46   {
47     std::cerr << e.what() << std::endl;
48   }
49 
50   return 0;
51 }
52