鱼C论坛

 找回密码
 立即注册
查看: 2498|回复: 0

[技术交流] 《C++Boost库asio》第七篇 异步time时间服务器

[复制链接]
发表于 2017-7-11 14:36:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
异步time时间服务器:
一次能连接多个客户端,处理多个客户端的事务

  1. // 异步time时间服务器:

  2. #include <boost/asio.hpp>
  3. //#include <boost\bind.hpp>//老式写法

  4. #include <iostream>
  5. #include <string>


  6. using boost::asio::ip::tcp;

  7. std::string make_daytime_string()
  8. {
  9.         using namespace std;//For time_t, time and ctime;
  10.                                                 //time_t now = time(0);
  11.         auto now = time(nullptr);
  12.         return ctime(&now); // 返回const char*
  13. }

  14. class tcp_connection : public std::enable_shared_from_this<tcp_connection> {
  15. public :
  16.         using pointer = std::shared_ptr<tcp_connection>;

  17.         static pointer create(boost::asio::io_service &io_service) {
  18.                 //std::make_shared
  19.                 return pointer(new tcp_connection(io_service));
  20.         }

  21.         tcp::socket &socket() { return socket_; }

  22.         void start() {
  23.                 message_ = make_daytime_string();//返回当前时间

  24.                 auto self = shared_from_this();

  25. #if (0) //老式写法
  26.                 boost::asio::async_write(//sync同步,async异步
  27.                         socket_, boost::asio::buffer(message_),
  28.                         boost::bind(
  29.                                 &tcp_connection::handle_write,
  30.                                 shared_from_this(),
  31.                                 boost::asio::placeholders::error,
  32.                                 boost::asio::placeholders::bytes_transferred));
  33. #endif

  34.                 boost::asio::async_write(
  35.                         socket_, boost::asio::buffer(message_),
  36.                         [self = std::move(self)](auto error, auto bytes_transferred){
  37.                         self->handle_write(error,bytes_transferred);
  38.                 });//用move不用引用是怕失效,同时省略掉一次拷贝
  39.         }

  40. private:
  41.         tcp_connection(boost::asio::io_service &io_service) : socket_(io_service){}

  42.         void handle_write(const boost::system::error_code & error, size_t bytes_transferred) {
  43.                 std::cout << "write succ \n" << std::endl;
  44.         }

  45.         tcp::socket socket_;
  46.         std::string message_;
  47.        
  48. };

  49. class tcp_server {
  50. public:
  51.         tcp_server(boost::asio::io_service &io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 2001)) {// 绑定端口
  52.                 start_accept();//等待新连接;
  53.         }

  54. private:
  55.         void start_accept() {
  56.                 //tcp_connection::pointer new_connection
  57.                 auto new_connection = tcp_connection::create(acceptor_.get_io_service());
  58.                 //异步的方式,开始接受客户端的连接,连接成功或失败进入回调
  59. #if (0)
  60.                 //老式的
  61.                 acceptor_.async_accept(new_connection->socket(),
  62.                         boost::bind(&tcp_server::handle_accept,
  63.                                 this, new_connection,
  64.                                 boost::asio::placeholders::error));
  65. #endif
  66.                 //新式的
  67.                 acceptor_.async_accept(new_connection->socket(),
  68.                         [this, new_connection](auto error)        {
  69.                         this->handle_accept(new_connection, error);
  70.                 });
  71.         }

  72.         void handle_accept(tcp_connection::pointer new_connection,
  73.                 const boost::system::error_code &error) {
  74.                 //new_connection是否连接已经成功的connection,error是否已经成功
  75.                 if (!error) {
  76.                         new_connection->start();
  77.                 }

  78.                 start_accept();
  79.         }

  80.         tcp::acceptor acceptor_;
  81. };

  82. int main()
  83. {
  84.         // 我们的程序和操作系统底层之间的一个相互传递的枢纽
  85.         // 会把事件按照一定方式进行处理;
  86.         boost::asio::io_service io;
  87.        
  88.         try
  89.         {
  90.        
  91.                 tcp_server server(io);

  92.                 io.run();

  93.         }
  94.         catch (std::exception &e)
  95.         {
  96.                 std::cerr << e.what() << std::endl;
  97.         }
  98.        

  99.         std::cout << "mian finish run \n" << std::endl;// 提示下main函数已经结束了
  100.         system("pause");// 让VS控制台程序不会一闪而过;
  101.     return 0;
  102. }
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
小甲鱼 + 2 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-21 00:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表