boost.asio框架系列之socket編程
asio的主要用途還是用于socket編程,本文就以一個tcp的daytimer服務(wù)為例簡單的演示一下如何實(shí)現(xiàn)同步和異步的tcp socket編程。
客戶端
客戶端的代碼如下:
#include <iostream> #include <boost/array.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; int main(int argc, char* argv[]) { try { boost::asio::io_service io_service; tcp::endpoint end_point(boost::asio::ip::address::from_string("127.0.0.1"), 3200); tcp::socketsocket(io_service); socket.connect(end_point); for (;;) { boost::array<char, 128> buf; boost::system::error_code error; size_t len = socket.read_some(boost::asio::buffer(buf), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. std::cout.write(buf.data(), len); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
主要流程如下:
通過tcp::socket類定義一個tcp client對象socket
通過connect函數(shù)連接服務(wù)器,打開socket連接。
通過read_some函數(shù)來讀數(shù)據(jù)
另外,還可以通過write_some來寫數(shù)據(jù),通過close來關(guān)閉socket連接(這里是通過釋放socket對象隱式釋放連接)。
服務(wù)器
服務(wù)器代碼如下:
#include <ctime> #include <iostream> #include <string> #include <boost/asio.hpp> using namespace boost; using boost::asio::ip::tcp; int main() { try { asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3200)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); time_t now = time(0); std::string message = ctime(&now); system::error_code ignored_error; socket.write_some(asio::buffer(message), ignored_error); } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
主要流程如下:
通過tcp::acceptor類創(chuàng)建一個tcp server對象,并綁定端口(也可以不在構(gòu)造器中自動綁定,而通過bind函數(shù)手動綁定)
通過accept函數(shù)獲取遠(yuǎn)端連接
通過遠(yuǎn)端連接的write_some函數(shù)將數(shù)據(jù)發(fā)往客戶端
異步服務(wù)器
前面的服務(wù)器是同步版本,在大并發(fā)的場景下一般需要用到異步socket。服務(wù)器的異步版本如下:
#include <ctime> #include <iostream> #include <string> #include <memory> #include <functional> #include <boost/asio.hpp> using boost::asio::ip::tcp; using namespace std; void process_client(shared_ptr<tcp::socket> client) { time_t now = time(0); shared_ptr<string> message(new string(ctime(&now))); auto callback = [=](const boost::system::error_code& err ,size_t size) { if ((int)size == message->length()) cout << "write completed" << endl; }; client->async_send(boost::asio::buffer(*message), callback); } typedef function<void (const boost::system::error_code&)> accept_callback; void start_accept(tcp::acceptor& server) { shared_ptr<tcp::socket> client(new tcp::socket(server.get_io_service())); accept_callback callback = [&server, client](const boost::system::error_code& error) { if (!error) process_client(client); start_accept(server); }; server.async_accept(*client, callback); } int main() { try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3200)); start_accept(acceptor); io_service.run(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } return 0; }
這個異步版本的邏輯倒不是很復(fù)雜,基本上和.net中傳統(tǒng)的異步socket相似,不過需要注意的是,由于c++中內(nèi)存需要自己管理,而asio框架也沒有提供任何管理機(jī)制,因此需要注意async_accept、async_send等函數(shù)的參數(shù)生命周期,切記不能在里面?zhèn)魅霔W兞康囊?。如果是堆變量,需要確保釋放,本例中我是通過share_ptr來實(shí)現(xiàn)的自動釋放。
到此這篇關(guān)于boost.asio框架系列之socket編程的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言中字符串常用函數(shù)strcat與strcpy的用法介紹
以下是對C語言中字符串常用函數(shù)strcat與strcpy的使用方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例
本文主要介紹了C++?自增自減運(yùn)算符的實(shí)現(xiàn)示例,自增和自減運(yùn)算符在C++中主要用于循環(huán)語句中,使循環(huán)變量的值自動+1或者-1,具有一定的參考價值,感興趣的可以了解一下2023-08-08Eclipse中C++連接mysql數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了Eclipse中C++連接mysql數(shù)據(jù)庫 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06探討編寫int strlen(char *strDest);不允許定義變量的問題
本篇文章是對編寫int strlen(char *strDest);不允許定義變量的問題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05簡單實(shí)現(xiàn)C++復(fù)數(shù)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C++簡單實(shí)現(xiàn)復(fù)數(shù)計(jì)算器的的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06C++中靜態(tài)成員函數(shù)訪問非靜態(tài)成員的實(shí)例
這篇文章主要介紹了C++中靜態(tài)成員函數(shù)訪問非靜態(tài)成員的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07