欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++ boost::asio編程-異步TCP詳解及實(shí)例代碼

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

C++ boost::asio編程-異步TCP

大家好,我是異步方式

和同步方式不同,我從來不花時間去等那些龜速的IO操作,我只是向系統(tǒng)說一聲要做什么,然后就可以做其它事去了。如果系統(tǒng)完成了操作, 系統(tǒng)就會通過我之前給它的回調(diào)對象來通知我。
在ASIO庫中,異步方式的函數(shù)或方法名稱前面都有“async_ ” 前綴,函數(shù)參數(shù)里會要求放一個回調(diào)函數(shù)(或仿函數(shù))。異步操作執(zhí)行 后不管有沒有完成都會立即返回,這時可以做一些其它事,直到回調(diào)函數(shù)(或仿函數(shù))被調(diào)用,說明異步操作已經(jīng)完成。

在ASIO中很多回調(diào)函數(shù)都只接受一個boost::system::error_code參數(shù),在實(shí)際使用時肯定是不夠的,所以一般 使用仿函數(shù)攜帶一堆相關(guān)數(shù)據(jù)作為回調(diào),或者使用boost::bind來綁定一堆數(shù)據(jù)。

另外要注意的是,只有io_service類的run()方法運(yùn)行之后回調(diào)對象才會被調(diào)用,否則即使系統(tǒng)已經(jīng)完成了異步操作也不會有任 務(wù)動作。

好了,就介紹到這里,下面是我?guī)淼漠惒椒绞絋CP Helloworld服務(wù)器端:

// BoostTcpServer.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。 
// 
 
#include "stdafx.h" 
#include "boost/asio.hpp" 
#include "boost/shared_ptr.hpp" 
#include "boost/thread.hpp" 
 
using namespace std; 
using namespace boost::asio; 
 
#ifdef _MSC_VER 
#define _WIN32_WINNT  0X0501 //避免VC下編譯警告 
#endif 
 
#define PORT 1000 
#define IPV6 
//#define IPV4 
 
class AsyncServer 
{ 
public: 
  //構(gòu)造函數(shù) 
  AsyncServer(io_service &io,ip::tcp::endpoint &ep):ios(io),acceptor(io,ep) 
  { 
    //acceptor(ios,ep); 
    start(); 
  } 
  //啟動異步接受客戶端連接 
  void start() 
  { 
    sock_ptr sock(new ip::tcp::socket(ios)); 
    //當(dāng)有連接進(jìn)入時回調(diào)accept_handler函數(shù) 
    acceptor.async_accept(*sock, 
      boost::bind(&AsyncServer::accept_handler,this,placeholders::error,sock)); 
  } 
private: 
  io_service &ios; 
  ip::tcp::acceptor acceptor; 
  typedef boost::shared_ptr<ip::tcp::socket> sock_ptr; 
 
  void accept_handler(const boost::system::error_code &ec, sock_ptr sock) 
  { 
    if(ec)  
      return; 
    //輸出客戶端連接信息 
    std::cout <<"remote ip:"<<sock->remote_endpoint().address()<<endl; 
    std::cout <<"remote port:"<<sock->remote_endpoint().port() << std::endl; 
    //異步向客戶端發(fā)送數(shù)據(jù),發(fā)送完成時調(diào)用write_handler 
    sock->async_write_some(buffer("I heard you!"), 
      bind(&AsyncServer::write_handler,this,placeholders::error)); 
    //再次啟動異步接受連接 
    start(); 
  } 
 
  void write_handler(const boost::system::error_code&) 
  { 
    cout<<"send msg complete!"<<endl; 
  } 
}; 
 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
  try 
  { 
    //定義io_service對象 
    io_service ios; 
    //定義服務(wù)端endpoint對象(協(xié)議和監(jiān)聽端口) 
#ifdef IPV4 
    ip::tcp::endpoint serverep(ip::tcp::v4(),PORT); 
#endif 
 
#ifdef IPV6 
    ip::tcp::endpoint serverep(ip::tcp::v6(),PORT); 
#endif 
    //啟動異步服務(wù) 
    AsyncServer server(ios, serverep); 
    //等待異步完成 
    ios.run(); 
  } 
  catch (std::exception& e) 
  { 
    cout<<e.what()<<endl; 
  } 
  return 0; 
} 

客戶端一般無需采用異步方式,同同步方式即可。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

上一篇:C語言 文件操作解析詳解及實(shí)例代碼

欄    目:C語言

下一篇:C語言位運(yùn)算和sizeof運(yùn)算符詳解

本文標(biāo)題:C++ boost::asio編程-異步TCP詳解及實(shí)例代碼

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1970.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有