日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

boost::asio异步模式的C/S客户端源码实现

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 boost::asio异步模式的C/S客户端源码实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

異步模式的服務(wù)器源碼

//g++ -g async_tcp_server.cpp -o async_tcp_server -lboost_system
//#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/system/error_code.hpp>using namespace std;
using namespace boost::asio;class server{private:io_service &ios;ip::tcp::acceptor acceptor;typedef boost::shared_ptr<ip::tcp::socket> sock_ptr;public:server(io_service& io): ios(io),acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688)){ start(); }void start(){sock_ptr sock(new ip::tcp::socket(ios));acceptor.async_accept(*sock, bind(&server::accept_handler, this, placeholders::error, sock));}void accept_handler(const boost::system::error_code& ec, sock_ptr sock){if(ec) return;cout << "client: ";cout << sock->remote_endpoint().address() << endl;sock->async_write_some(buffer("hello asio"), bind(&server::write_handler, this, placeholders::error));start(); //retry to accept the next quest ......}void write_handler(const boost::system::error_code&){cout << "send msg complete." << endl;}
};int main(){try{cout << "async tcp server start ......" << endl;io_service ios;server serv(ios);ios.run();}catch(std::exception& e){cout << e.what() << endl;}
}

異步模式的客戶端源碼

//g++ -g async_tcp_client.cpp -o async_tcp_client -lboost_system -lboost_date_time
//#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>using namespace std;
using namespace boost::asio;class client{private:io_service& ios;ip::tcp::endpoint ep;typedef boost::shared_ptr<ip::tcp::socket> sock_ptr;public:client(io_service& io): ios(io),ep(ip::address::from_string("127.0.0.1"), 6688){start();}void start(){sock_ptr sock(new ip::tcp::socket(ios));sock->async_connect(ep, bind(&client::conn_handler, this, placeholders::error, sock));}void conn_handler(const boost::system::error_code& ec, sock_ptr sock){if(ec)  return;cout << "receive from " << sock->remote_endpoint().address() << endl;boost::shared_ptr<vector<char> > str(new vector<char>(100, 0));sock->async_read_some(buffer(*str), bind(&client::read_handler, this, placeholders::error, str));//sync timer block to control the client connection frequencydeadline_timer t(ios, boost::posix_time::seconds(5));t.wait();start(); //retry to connect in the next time}void read_handler(const boost::system::error_code& ec, boost::shared_ptr<vector<char> > str){if(ec) return;cout << &(*str)[0] << endl;}
};int main()
{try{cout << "client start ......" << endl;io_service ios;client cl(ios);ios.run();return 0;}catch(exception& e){cout << e.what() << endl;}
}

運(yùn)行效果截圖



參考文獻(xiàn)

[1].羅劍鋒, Boost程序庫完全開發(fā)指南---深入C++"準(zhǔn)"標(biāo)準(zhǔn)庫

總結(jié)

以上是生活随笔為你收集整理的boost::asio异步模式的C/S客户端源码实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。