Advertisement
obernardovieira

(Basic) Client VS Server (boost)

Sep 21st, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. //CLIENT
  2.  
  3. #pragma warning(disable : 4996)
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <boost/asio.hpp>
  8.  
  9. using boost::asio::ip::tcp;
  10.  
  11. int main(int argc, char* argv[])
  12. {
  13.     try
  14.     {
  15.         if (argc != 2)
  16.         {
  17.             std::cerr << "Usage: daytime_client <host>" << std::endl;
  18.             return 1;
  19.         }
  20.  
  21.         tcp::iostream s(argv[1], "daytime");
  22.         std::string line;
  23.         std::getline(s, line);
  24.         std::cout << line << std::endl;
  25.     }
  26.     catch (std::exception& e)
  27.     {
  28.         std::cout << "Exception: " << e.what() << std::endl;
  29.     }
  30.  
  31.     return 0;
  32. }
  33.  
  34. //SERVER
  35.  
  36. #pragma warning(disable : 4996)
  37.  
  38. #include <ctime>
  39. #include <iostream>
  40. #include <string>
  41. #include <boost/asio.hpp>
  42.  
  43. using boost::asio::ip::tcp;
  44.  
  45. std::string make_daytime_string()
  46. {
  47.     using namespace std; // For time_t, time and ctime;
  48.     time_t now = time(0);
  49.     return ctime(&now);
  50. }
  51.  
  52. int main()
  53. {
  54.     try
  55.     {
  56.         boost::asio::io_service io_service;
  57.  
  58.         tcp::endpoint endpoint(tcp::v4(), 13);
  59.         tcp::acceptor acceptor(io_service, endpoint);
  60.  
  61.         for (;;)
  62.         {
  63.             tcp::iostream stream;
  64.             acceptor.accept(*stream.rdbuf());
  65.             stream << make_daytime_string();
  66.         }
  67.     }
  68.     catch (std::exception& e)
  69.     {
  70.         std::cerr << e.what() << std::endl;
  71.     }
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement