Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. //
  2. // server.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 "pch.h"
  12. #include <ctime>
  13. #include <iostream>
  14. #include <string>
  15. #include <boost/asio.hpp>
  16.  
  17. using boost::asio::ip::tcp;
  18.  
  19. std::string make_daytime_string()
  20. {
  21. using namespace std; // For time_t, time and ctime;
  22. time_t now = time(0);
  23. return ctime(&now);
  24. }
  25.  
  26. int main()
  27. {
  28. try
  29. {
  30. boost::asio::io_service io_service;
  31.  
  32. tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
  33.  
  34. for (;;)
  35. {
  36. tcp::socket socket(io_service);
  37. acceptor.accept(socket);
  38.  
  39. std::string message = make_daytime_string();
  40.  
  41. boost::system::error_code ignored_error;
  42. boost::asio::write(socket, boost::asio::buffer(message),
  43. boost::asio::transfer_all(), ignored_error);
  44. }
  45. }
  46. catch (std::exception& e)
  47. {
  48. std::cerr << e.what() << std::endl;
  49. }
  50.  
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement