DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
  1. // test.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <boost/asio.hpp>  
  6. #include <boost/bind.hpp>  
  7. #include <boost/date_time/posix_time/posix_time_types.hpp>  
  8. #include <iostream>  
  9.   
  10. using namespace boost::asio;  
  11. using boost::asio::ip::tcp;  
  12.   
  13. class connect_handler  
  14. {  
  15. public:  
  16.     connect_handler(io_service& ios)  
  17.         : io_service_(ios),  
  18.         timer_(ios),  
  19.         socket_(ios)  
  20.     {  
  21.         socket_.async_connect(  
  22.             tcp::endpoint(boost::asio::ip::address_v4::loopback(), 3212),  
  23.             boost::bind(&connect_handler::handle_connect, this,  
  24.             boost::asio::placeholders::error));  
  25.   
  26.         timer_.expires_from_now(boost::posix_time::seconds(5));  
  27.         timer_.async_wait(boost::bind(&connect_handler::close, this));  
  28.     }  
  29.   
  30.     void handle_connect(const boost::system::error_code& err)  
  31.     {  
  32.         if (err)  
  33.         {  
  34.             std::cout << "Connect error: " << err.message() << "\n";  
  35.         }  
  36.         else  
  37.         {  
  38.             std::cout << "Successful connection\n";  
  39.         }  
  40.     }  
  41.   
  42.     void close()  
  43.     {  
  44.         socket_.close();  
  45.     }  
  46.   
  47. private:  
  48.     io_service& io_service_;  
  49.     deadline_timer timer_;  
  50.     tcp::socket socket_;  
  51. };  
  52.   
  53. int main()  
  54. {  
  55.     try  
  56.     {  
  57.         io_service ios;  
  58.         tcp::acceptor a(ios, tcp::endpoint(tcp::v4(), 32123), 1);  
  59.   
  60.         // Make lots of connections so that at least some of them will block.  
  61.         connect_handler ch1(ios);  
  62.         //connect_handler ch2(ios);  
  63.         //connect_handler ch3(ios);  
  64.         //connect_handler ch4(ios);  
  65.         //connect_handler ch5(ios);  
  66.         //connect_handler ch6(ios);  
  67.         //connect_handler ch7(ios);  
  68.         //connect_handler ch8(ios);  
  69.         //connect_handler ch9(ios);  
  70.   
  71.         ios.run();  
  72.     }  
  73.     catch (std::exception& e)  
  74.     {  
  75.         std::cerr << "Exception: " << e.what() << "\n";  
  76.     }  
  77.   
  78.     return 0;  
  79. }  
posted on   DoubleLi  阅读(6204)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示