boost::asio::ip::tcp::resolver的故事

时间:2022-09-09 11:38:27

出处:http://www.cppblog.com/Error/archive/2012/11/07/194839.html?opt=admin

直白点说,就是对getaddrinfo()这个函数的适配。抽象点说就是解析器。

细节如下:

boost::asio::ip::tcp::resolver resolver(asioService);
boost::asio::ip::tcp::resolver::query queryEndpoints(boost::asio::ip::host_name(),"80");
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(queryEndpoints);
;
for(boost::asio::ip::tcp::resolver::iterator iterNull;
    endpoint_iterator != iterNull;
    endpoint_iterator++)
{
    std::cout << endpoint_iterator->endpoint() << std::endl;
}

 

上面的代码有这么几个类型:

boost::asio::ip::tcp::resolver

boost::asio::ip::tcp::resolver::query

boost::asio::ip::tcp::resolver::iterator

 

resolver抽线的是getaddrinfo()动作

boost::asio::ip::tcp::resolver::query抽象的是getaddrinfo()需要的参数

boost::asio::ip::tcp::resolver::iterator抽象的是getaddrinfo()的结果

这整个体系是抽象winsock sdk到stl思想