server.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // 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/bind.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/enable_shared_from_this.hpp>
  16. #include <boost/asio.hpp>
  17. using boost::asio::ip::tcp;
  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. class tcp_connection
  25. : public boost::enable_shared_from_this<tcp_connection>
  26. {
  27. public:
  28. typedef boost::shared_ptr<tcp_connection> pointer;
  29. static pointer create(boost::asio::io_context& io_context)
  30. {
  31. return pointer(new tcp_connection(io_context));
  32. }
  33. tcp::socket& socket()
  34. {
  35. return socket_;
  36. }
  37. void start()
  38. {
  39. message_ = make_daytime_string();
  40. boost::asio::async_write(socket_, boost::asio::buffer(message_),
  41. boost::bind(&tcp_connection::handle_write, shared_from_this(),
  42. boost::asio::placeholders::error,
  43. boost::asio::placeholders::bytes_transferred));
  44. }
  45. private:
  46. tcp_connection(boost::asio::io_context& io_context)
  47. : socket_(io_context)
  48. {
  49. }
  50. void handle_write(const boost::system::error_code& /*error*/,
  51. size_t /*bytes_transferred*/)
  52. {
  53. }
  54. tcp::socket socket_;
  55. std::string message_;
  56. };
  57. class tcp_server
  58. {
  59. public:
  60. tcp_server(boost::asio::io_context& io_context)
  61. : io_context_(io_context),
  62. acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
  63. {
  64. start_accept();
  65. }
  66. private:
  67. void start_accept()
  68. {
  69. tcp_connection::pointer new_connection =
  70. tcp_connection::create(io_context_);
  71. acceptor_.async_accept(new_connection->socket(),
  72. boost::bind(&tcp_server::handle_accept, this, new_connection,
  73. boost::asio::placeholders::error));
  74. }
  75. void handle_accept(tcp_connection::pointer new_connection,
  76. const boost::system::error_code& error)
  77. {
  78. if (!error)
  79. {
  80. new_connection->start();
  81. }
  82. start_accept();
  83. }
  84. boost::asio::io_context& io_context_;
  85. tcp::acceptor acceptor_;
  86. };
  87. int main()
  88. {
  89. try
  90. {
  91. boost::asio::io_context io_context;
  92. tcp_server server(io_context);
  93. io_context.run();
  94. }
  95. catch (std::exception& e)
  96. {
  97. std::cerr << e.what() << std::endl;
  98. }
  99. return 0;
  100. }