=================================版權聲明=================================
版權聲明:原創文章 謝絕轉載 啥說的,鄙視那些無視版權隨意抓取博文的爬蟲小網站,祝你們早升極樂。
請通過右側公告中的“聯系郵箱([email protected])”聯系我
勿用於學術性引用。
勿用於商業出版、商業印刷、商業引用以及其他商業用途。
本文不定期修正完善。
本文鏈接:http://www.cnblogs.com/wlsandwho/p/5063318.html
恥辱牆:http://www.cnblogs.com/wlsandwho/p/4206472.html
=======================================================================
鑒於我早已看過Windows核心編程這本書,下面的例子就不需要再做詳細解釋了。
代碼來自Boost手冊,略作修改以方便閱讀。
環境:VS2010+win7。
因為我電腦開啟了防火牆,所以代碼中使用6666作為通訊端口。
=======================================================================
Timer1 同步地使用定時器
1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/date_time/posix_time/posix_time.hpp> 4 5 int main() 6 { 7 boost::asio::io_service IOSrv; 8 9 boost::asio::deadline_timer DeadlineTimer(IOSrv,boost::posix_time::seconds(5)); 10 11 std::cout<<"-----------------------"<<std::endl; 12 13 DeadlineTimer.wait(); 14 15 std::cout<<"-----------------------"<<std::endl; 16 17 std::cout<<"Hello Boost.Asio!"<<std::endl; 18 19 system("Pause"); 20 21 return 0; 22 }
=======================================================================
Timer2 異步地使用定時器
1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/date_time/posix_time/posix_time.hpp> 4 5 void Print(const boost::system::error_code&) 6 { 7 std::cout<<"Hello Boost.Asio!"<<std::endl; 8 } 9 10 int main() 11 { 12 boost::asio::io_service IOSrv; 13 14 boost::asio::deadline_timer DeadlineTimer(IOSrv,boost::posix_time::seconds(5)); 15 16 DeadlineTimer.async_wait(&Print); 17 18 std::cout<<"-----------------------"<<std::endl; 19 20 IOSrv.run(); 21 22 std::cout<<"-----------------------"<<std::endl; 23 24 system("Pause"); 25 26 return 0; 27 }
=======================================================================
Timer3 給處理程序綁定參數
1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/bind.hpp> 4 #include <boost/date_time/posix_time/posix_time.hpp> 5 6 7 void Print(const boost::system::error_code&,boost::asio::deadline_timer* pDeadlineTimer,int* pnCount) 8 { 9 if (*pnCount<5) 10 { 11 std::cout<<*pnCount<<std::endl; 12 ++(*pnCount); 13 14 pDeadlineTimer->expires_at(pDeadlineTimer->expires_at()+boost::posix_time::seconds(1)); 15 16 pDeadlineTimer->async_wait(boost::bind(Print,boost::asio::placeholders::error,pDeadlineTimer,pnCount)); 17 } 18 } 19 20 int main() 21 { 22 boost::asio::io_service IOSrv; 23 24 int nCount=0; 25 26 boost::asio::deadline_timer DeadlineTimer(IOSrv,boost::posix_time::seconds(1)); 27 28 DeadlineTimer.async_wait(boost::bind(Print,boost::asio::placeholders::error,&DeadlineTimer,&nCount)); 29 30 std::cout<<"-----------------------"<<nCount<<"-----------------------"<<std::endl; 31 32 IOSrv.run(); 33 34 std::cout<<"-----------------------"<<nCount<<"-----------------------"<<std::endl; 35 36 system("Pause"); 37 38 return 0; 39 }
=======================================================================
Timer4 將成員函數作為處理程序
1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/bind.hpp> 4 #include <boost/date_time/posix_time/posix_time.hpp> 5 6 7 class CPrinter 8 { 9 public: 10 CPrinter(boost::asio::io_service& IOSrv):m_DeadlineTimer(IOSrv,boost::posix_time::seconds(1)),m_nCount(0) 11 { 12 std::cout<<"-----------------------"<<m_nCount<<"-----------------------1"<<std::endl; 13 14 m_DeadlineTimer.async_wait(boost::bind(&CPrinter::Print,this)); 15 std::cout<<"-----------------------"<<m_nCount<<"-----------------------2"<<std::endl; 16 } 17 18 ~CPrinter() 19 { 20 std::cout<<"-----------------------"<<m_nCount<<"-----------------------3"<<std::endl; 21 } 22 23 void Print() 24 { 25 if (m_nCount<5) 26 { 27 std::cout<<m_nCount<<std::endl; 28 29 ++m_nCount; 30 31 m_DeadlineTimer.expires_at(m_DeadlineTimer.expires_at()+boost::posix_time::seconds(1)); 32 33 m_DeadlineTimer.async_wait(boost::bind(&CPrinter::Print,this)); 34 } 35 } 36 37 int GetCount(){return m_nCount;} 38 39 private: 40 int m_nCount; 41 boost::asio::deadline_timer m_DeadlineTimer; 42 }; 43 44 int main() 45 { 46 boost::asio::io_service IOSrv; 47 48 CPrinter* pPrinter=new CPrinter(IOSrv); 49 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------4"<<std::endl; 50 IOSrv.run(); 51 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------5"<<std::endl; 52 delete pPrinter; 53 54 system("pause"); 55 56 return 0; 57 }
=======================================================================
Timer5 多線程程序中的同步處理程序
1 #include <iostream> 2 #include <boost/asio.hpp> 3 #include <boost/thread/thread.hpp> 4 #include <boost/bind.hpp> 5 #include <boost/date_time/posix_time/posix_time.hpp> 6 7 class CPrinter 8 { 9 public: 10 CPrinter(boost::asio::io_service& IOSrv):m_IOSrvStrand(IOSrv), 11 m_DeadlineTimer1(IOSrv,boost::posix_time::seconds(1)), 12 m_DeadlineTimer2(IOSrv,boost::posix_time::seconds(1)), 13 m_nCount(0) 14 { 15 std::cout<<"-----------------------"<<m_nCount<<"-----------------------1"<<std::endl; 16 17 m_DeadlineTimer1.async_wait(m_IOSrvStrand.wrap(boost::bind(&CPrinter::Print1,this))); 18 m_DeadlineTimer2.async_wait(m_IOSrvStrand.wrap(boost::bind(&CPrinter::Print2,this))); 19 20 std::cout<<"-----------------------"<<m_nCount<<"-----------------------2"<<std::endl; 21 } 22 23 ~CPrinter() 24 { 25 std::cout<<"-----------------------"<<m_nCount<<"-----------------------3"<<std::endl; 26 } 27 28 void Print1() 29 { 30 if (m_nCount<10) 31 { 32 std::cout<<"Timer1:"<<m_nCount<<std::endl; 33 34 ++m_nCount; 35 36 m_DeadlineTimer1.expires_at(m_DeadlineTimer1.expires_at()+boost::posix_time::seconds(1)); 37 m_DeadlineTimer1.async_wait(m_IOSrvStrand.wrap(boost::bind(&CPrinter::Print1,this))); 38 } 39 } 40 41 void Print2() 42 { 43 if (m_nCount<10) 44 { 45 std::cout<<" Timer2:"<<m_nCount<<std::endl; 46 47 ++m_nCount; 48 49 m_DeadlineTimer2.expires_at(m_DeadlineTimer2.expires_at()+boost::posix_time::seconds(1)); 50 m_DeadlineTimer2.async_wait(m_IOSrvStrand.wrap(boost::bind(&CPrinter::Print2,this))); 51 } 52 } 53 54 int GetCount(){return m_nCount;} 55 56 private: 57 boost::asio::io_service::strand m_IOSrvStrand; 58 boost::asio::deadline_timer m_DeadlineTimer1; 59 boost::asio::deadline_timer m_DeadlineTimer2; 60 61 int m_nCount; 62 }; 63 64 int main() 65 { 66 boost::asio::io_service IOSrv; 67 68 CPrinter* pPrinter=new CPrinter(IOSrv); 69 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------4"<<std::endl; 70 71 boost::thread ThreadforPrinter(boost::bind(&boost::asio::io_service::run,&IOSrv)); 72 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------5"<<std::endl; 73 74 IOSrv.run(); 75 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------6"<<std::endl; 76 77 ThreadforPrinter.join(); 78 std::cout<<"-----------------------"<<pPrinter->GetCount()<<"-----------------------7"<<std::endl; 79 80 delete pPrinter; 81 82 system("Pause"); 83 84 return 0; 85 }
=======================================================================
Daytime1 使用同步Tcp的請求daytime服務的客戶端
1 #include <iostream> 2 #include <boost/array.hpp> 3 #include <boost/asio.hpp> 4 5 using boost::asio::ip::tcp; 6 7 int main() 8 { 9 try 10 { 11 std::cout<<"Enter The host IP,such as 192.168.0.26:"; 12 std::string strHost; 13 std::cin>>strHost; 14 15 boost::asio::io_service IOSrv; 16 17 tcp::resolver Resolver(IOSrv); 18 tcp::resolver::query Query(strHost,"6666");//tcp::resolver::query query(argv[1], "daytime"); 19 tcp::resolver::iterator EndPointIt=Resolver.resolve(Query); 20 21 tcp::socket Socket(IOSrv); 22 boost::asio::connect(Socket,EndPointIt); 23 24 for (;;) 25 { 26 boost::array<char,27> arrBuf;//boost::array<char, 128> buf; 27 boost::system::error_code Error; 28 29 size_t Len=Socket.read_some(boost::asio::buffer(arrBuf),Error); 30 31 if (Error==boost::asio::error::eof) 32 { 33 break; 34 } 35 else 36 if (Error) 37 { 38 throw boost::system::system_error(Error); 39 } 40 41 // for (int i=0;i<arrBuf.size();i++) 42 // { 43 // std::cout<<arrBuf.at(i); 44 // } 45 std::cout.write(arrBuf.data(),Len); 46 } 47 } 48 catch(std::exception& e) 49 { 50 std::cerr<<e.what()<<std::endl; 51 } 52 53 system("pause"); 54 }
client:Daytime1
Server:Daytime2
(注意:client退出是因為Server的代碼中,存儲客戶信息用的是臨時變量。)
=======================================================================
Daytime2 使用同步Tcp的提供daytime服務的服務端
1 #include <iostream> 2 #include <ctime> 3 #include <string> 4 #include <boost/asio.hpp> 5 6 using boost::asio::ip::tcp; 7 8 std::string MakeDaytimeStr() 9 { 10 using namespace std; 11 12 time_t TimeNow=time(0); 13 14 return ctime(&TimeNow); 15 } 16 17 int main() 18 { 19 try 20 { 21 boost::asio::io_service IOSrv; 22 23 tcp::acceptor Acceptor(IOSrv,tcp::endpoint(tcp::v4(),6666)); 24 25 for (;;) 26 { 27 tcp::socket Socket(IOSrv);//局部變量 28 Acceptor.accept(Socket); 29 30 std::string strMsg=MakeDaytimeStr(); 31 32 boost::system::error_code IgnoredErr; 33 boost::asio::write(Socket,boost::asio::buffer(strMsg),IgnoredErr); 34 } 35 } 36 catch (std::exception &e) 37 { 38 std::cerr<<e.what()<<std::endl; 39 } 40 41 system("pause"); 42 43 return 0; 44 }
=======================================================================
Daytime3 使用異步Tcp的提供daytime服務的服務端
1 #include <iostream> 2 #include <ctime> 3 #include <string> 4 #include <boost/asio.hpp> 5 #include <boost/bind.hpp> 6 #include <boost/shared_ptr.hpp> 7 #include <boost/enable_shared_from_this.hpp> 8 9 using boost::asio::ip::tcp; 10 11 std::string MakeDaytimeStr() 12 { 13 std::cout<<"-----------------------------------------------1"<<std::endl; 14 using namespace std; 15 16 time_t TimeNow=time(0); 17 18 return ctime(&TimeNow); 19 } 20 21 class CTcpConnection 22 :public boost::enable_shared_from_this<CTcpConnection> 23 { 24 public: 25 typedef boost::shared_ptr<CTcpConnection> TcpClientPtr; 26 27 static TcpClientPtr Create(boost::asio::io_service& IOSrv)//創建 28 { 29 std::cout<<"-----------------------------------------------2"<<std::endl; 30 return TcpClientPtr(new CTcpConnection(IOSrv)); 31 } 32 33 tcp::socket& Socket() 34 { 35 std::cout<<"-----------------------------------------------3"<<std::endl; 36 return m_Socket; 37 } 38 39 void Start()//異步寫 40 { 41 std::cout<<"-----------------------------------------------4"<<std::endl; 42 m_strMsg=MakeDaytimeStr(); 43 44 std::cout<<"-----------------------------------------------5"<<std::endl; 45 boost::asio::async_write(m_Socket, 46 boost::asio::buffer(m_strMsg), 47 boost::bind(&CTcpConnection::HandleWrite, 48 shared_from_this(), 49 boost::asio::placeholders::error, 50 boost::asio::placeholders::bytes_transferred)); 51 } 52 53 private: 54 CTcpConnection(boost::asio::io_service& IOSrv):m_Socket(IOSrv) 55 { 56 std::cout<<"-----------------------------------------------6"<<std::endl; 57 } 58 59 void HandleWrite(const boost::system::error_code&,size_t) 60 { 61 std::cout<<"-----------------------------------------------7"<<std::endl; 62 } 63 64 private: 65 tcp::socket m_Socket; 66 std::string m_strMsg; 67 }; 68 69 class CTcpServer 70 { 71 public: 72 CTcpServer(boost::asio::io_service& IOSrv):m_Acceptor(IOSrv,tcp::endpoint(tcp::v4(),6666)) 73 { 74 std::cout<<"-----------------------------------------------8"<<std::endl; 75 StartAccept(); 76 } 77 78 private: 79 void StartAccept() 80 { 81 std::cout<<"-----------------------------------------------9"<<std::endl; 82 CTcpConnection::TcpClientPtr poNewClient=CTcpConnection::Create(m_Acceptor.get_io_service()); 83 84 std::cout<<"-----------------------------------------------10"<<std::endl; 85 m_Acceptor.async_accept(poNewClient->Socket(), 86 boost::bind(&CTcpServer::HandleAccept, 87 this, 88 poNewClient, 89 boost::asio::placeholders::error)); 90 } 91 92 void HandleAccept(CTcpConnection::TcpClientPtr poNewClient,const boost::system::error_code& Error) 93 { 94 std::cout<<"-----------------------------------------------11"<<std::endl; 95 if (!Error) 96 { 97 poNewClient->Start(); 98 } 99 100 std::cout<<"-----------------------------------------------12"<<std::endl; 101 StartAccept();//重新發起一次accept進行等待 102 } 103 104 private: 105 tcp::acceptor m_Acceptor; 106 }; 107 108 int main() 109 { 110 try 111 { 112 boost::asio::io_service IOSrv; 113 CTcpServer oTcpSrv(IOSrv); 114 115 IOSrv.run(); 116 std::cout<<"-----------------------------------------------13"<<std::endl; 117 } 118 catch(std::exception& e) 119 { 120 std::cerr<<e.what()<<std::endl; 121 } 122 123 system("pause"); 124 125 return 0; 126 }
連接前:
連接後:
=======================================================================
Daytime4 使用同步Udp請求daytime服務的客戶端
1 #include <iostream> 2 #include <boost/array.hpp> 3 #include <boost/asio.hpp> 4 5 using boost::asio::ip::udp; 6 7 8 int main() 9 { 10 try 11 { 12 std::cout<<"Enter The host IP,such as 192.168.0.26:"; 13 std::string strHost; 14 std::cin>>strHost; 15 16 boost::asio::io_service IOSrv; 17 18 udp::resolver Resolver(IOSrv); 19 udp::resolver::query Query(udp::v4(),strHost,"6666"); 20 21 udp::endpoint RecvEndPoint=*Resolver.resolve(Query); 22 23 udp::socket Socket(IOSrv); 24 Socket.open(udp::v4()); 25 26 boost::array<char,1> SendBuf={{0}}; 27 Socket.send_to(boost::asio::buffer(SendBuf),RecvEndPoint); 28 29 udp::endpoint SendEndPoint; 30 31 boost::array<char,27> RecvBuf; 32 size_t len=Socket.receive_from(boost::asio::buffer(RecvBuf),SendEndPoint); 33 34 std::cout.write(RecvBuf.data(),len); 35 36 } 37 catch (std::exception& e) 38 { 39 std::cerr<<e.what()<<std::endl; 40 } 41 42 system("pause"); 43 44 return 0; 45 }
client:Daytime4
server:Daytime5
=======================================================================
Daytime5 使用同步Udp提供daytime服務的服務端
1 #include <ctime> 2 #include <iostream> 3 #include <boost/array.hpp> 4 #include <boost/asio.hpp> 5 6 7 using boost::asio::ip::udp; 8 9 std::string MakeDaytimeStr() 10 { 11 using namespace std; 12 time_t TimeNow=time(0); 13 return ctime(&TimeNow); 14 } 15 16 int main() 17 { 18 try 19 { 20 boost::asio::io_service IOSrv; 21 22 udp::socket Socket(IOSrv,udp::endpoint(udp::v4(),6666)); 23 24 for (;;) 25 { 26 boost::array<char,1> RecvBuf; 27 28 udp::endpoint ClientEndPoint; 29 30 boost::system::error_code Error; 31 Socket.receive_from(boost::asio::buffer(RecvBuf),ClientEndPoint,0,Error); 32 if (Error && Error!=boost::asio::error::message_size) 33 { 34 throw boost::system::system_error(Error); 35 } 36 37 std::string strMsg=MakeDaytimeStr(); 38 39 boost::system::error_code ErrorIgnored; 40 Socket.send_to(boost::asio::buffer(strMsg),ClientEndPoint,0,ErrorIgnored); 41 } 42 43 } 44 catch(std::exception& e) 45 { 46 std::cerr<<e.what()<<std::endl; 47 } 48 49 return 0; 50 }
=======================================================================
Daytime6 使用異步Udp提供daytime服務的服務端
1 #include <ctime> 2 #include <iostream> 3 #include <boost/array.hpp> 4 #include <boost/bind.hpp> 5 #include <boost/shared_ptr.hpp> 6 #include <boost/asio.hpp> 7 8 using boost::asio::ip::udp; 9 10 std::string MakeDaytimeStr() 11 { 12 std::cout<<"-----------------------------------------------1"<<std::endl; 13 using namespace std; 14 time_t TimeNow=time(0); 15 16 return ctime(&TimeNow); 17 } 18 19 class CUdpServer 20 { 21 public: 22 CUdpServer(boost::asio::io_service& IOSrv):m_Socket(IOSrv,udp::endpoint(udp::v4(),6666)) 23 { 24 std::cout<<"-----------------------------------------------2"<<std::endl; 25 StartReceive(); 26 } 27 28 private: 29 void StartReceive() 30 { 31 std::cout<<"-----------------------------------------------3"<<std::endl; 32 m_Socket.async_receive_from(boost::asio::buffer(m_RecvBuf), 33 m_ClientEndPoint, 34 boost::bind(&CUdpServer::HandleReceive, 35 this, 36 boost::asio::placeholders::error, 37 boost::asio::placeholders::bytes_transferred)); 38 } 39 40 void HandleReceive(const boost::system::error_code& Error,std::size_t) 41 { 42 if (!Error || Error==boost::asio::error::message_size) 43 { 44 std::cout<<"-----------------------------------------------4"<<std::endl; 45 boost::shared_ptr<std::string> StrMsg(new std::string(MakeDaytimeStr())); 46 std::cout<<"-----------------------------------------------5"<<std::endl; 47 m_Socket.async_send_to(boost::asio::buffer(*StrMsg), 48 m_ClientEndPoint, 49 boost::bind(&CUdpServer::HandleSend, 50 this, 51 StrMsg, 52 boost::asio::placeholders::error, 53 boost::asio::placeholders::bytes_transferred)); 54 55 std::cout<<"-----------------------------------------------6"<<std::endl; 56 StartReceive(); 57 } 58 } 59 60 void HandleSend(boost::shared_ptr<std::string>,const boost::system::error_code&,std::size_t) 61 { 62 std::cout<<"-----------------------------------------------7"<<std::endl; 63 } 64 65 private: 66 udp::socket m_Socket; 67 udp::endpoint m_ClientEndPoint; 68 boost::array<char,1> m_RecvBuf; 69 }; 70 71 72 int main() 73 { 74 try 75 { 76 boost::asio::io_service IOSrv; 77 78 CUdpServer oUdpServer(IOSrv); 79 80 IOSrv.run(); 81 } 82 catch(std::exception& e) 83 { 84 std::cerr<<e.what()<<std::endl; 85 } 86 87 return 0; 88 }
=======================================================================
Daytime7 同時使用異步Tcp和異步Udp提供daytime服務的服務端
1 #include <ctime> 2 #include <iostream> 3 #include <string> 4 #include <boost/array.hpp> 5 #include <boost/bind.hpp> 6 #include <boost/shared_ptr.hpp> 7 #include <boost/enable_shared_from_this.hpp> 8 #include <boost/asio.hpp> 9 10 using boost::asio::ip::tcp; 11 using boost::asio::ip::udp; 12 13 std::string MakeDaytimeStr() 14 { 15 using namespace std; 16 17 time_t TimeNow=time(0); 18 19 return ctime(&TimeNow); 20 } 21 22 23 class CTcpConnection 24 :public boost::enable_shared_from_this<CTcpConnection> 25 { 26 public: 27 typedef boost::shared_ptr<CTcpConnection> PTcpClient; 28 29 static PTcpClient Create(boost::asio::io_service& IOSrv) 30 { 31 return PTcpClient(new CTcpConnection(IOSrv)); 32 } 33 34 tcp::socket& Socket() 35 { 36 return m_Socket; 37 } 38 39 void Start() 40 { 41 m_strMsg=MakeDaytimeStr(); 42 43 boost::asio::async_write(m_Socket, 44 boost::asio::buffer(m_strMsg), 45 boost::bind(&CTcpConnection::HandleWrite, 46 shared_from_this())); 47 } 48 49 private: 50 CTcpConnection(boost::asio::io_service& IOSrv):m_Socket(IOSrv) 51 { 52 53 } 54 55 void HandleWrite() 56 { 57 58 } 59 60 private: 61 tcp::socket m_Socket; 62 std::string m_strMsg; 63 }; 64 65 class CTcpServer 66 { 67 public: 68 CTcpServer(boost::asio::io_service& IOSrv):m_Acceptor(IOSrv,tcp::endpoint(tcp::v4(),6666)) 69 { 70 StartAccept(); 71 } 72 73 private: 74 void StartAccept() 75 { 76 CTcpConnection::PTcpClient poTcpClient=CTcpConnection::Create(m_Acceptor.get_io_service()); 77 78 m_Acceptor.async_accept(poTcpClient->Socket(), 79 boost::bind(&CTcpServer::HandleAccept, 80 this, 81 poTcpClient, 82 boost::asio::placeholders::error)); 83 } 84 85 void HandleAccept(CTcpConnection::PTcpClient poNewTcpClent,const boost::system::error_code& Error) 86 { 87 if (!Error) 88 { 89 poNewTcpClent->Start(); 90 } 91 92 StartAccept(); 93 } 94 95 private: 96 tcp::acceptor m_Acceptor; 97 }; 98 99 100 class CUdpServer 101 { 102 public: 103 CUdpServer(boost::asio::io_service& IOSrv):m_Socket(IOSrv,udp::endpoint(udp::v4(),6666)) 104 { 105 StartReceive(); 106 } 107 108 private: 109 void StartReceive() 110 { 111 m_Socket.async_receive_from(boost::asio::buffer(m_strMsg), 112 m_ClientEndPoint, 113 boost::bind(&CUdpServer::HandleReceive, 114 this, 115 boost::asio::placeholders::error)); 116 } 117 118 void HandleReceive(const boost::system::error_code& Error) 119 { 120 if (!Error || Error==boost::asio::error::message_size) 121 { 122 boost::shared_ptr<std::string> strMsg(new std::string(MakeDaytimeStr())); 123 124 m_Socket.async_send_to(boost::asio::buffer(*strMsg), 125 m_ClientEndPoint, 126 boost::bind(&CUdpServer::HandleSend, 127 this, 128 strMsg)); 129 StartReceive(); 130 } 131 } 132 133 void HandleSend(boost::shared_ptr<std::string>) 134 { 135 136 } 137 138 private: 139 udp::socket m_Socket; 140 udp::endpoint m_ClientEndPoint; 141 boost::array<char,1> m_strMsg; 142 }; 143 144 int main() 145 { 146 try 147 { 148 boost::asio::io_service IOSrv; 149 150 CTcpServer oTcpServer(IOSrv); 151 CUdpServer oUdpServer(IOSrv); 152 153 IOSrv.run(); 154 } 155 catch (std::exception& e) 156 { 157 std::cerr<<e.what()<<std::endl; 158 } 159 }
=======================================================================
(友情支持請掃描這個)
微信掃描上方二維碼捐贈
=======================================================================
點擊下載文中源碼。