daytime_server.cpp 997 B

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