示例代码如下:
#include <iostream>
using namespace std;
#include <boost/asio/ip/tcp.hpp >
#include <boost/asio.hpp>
using namespace boost;
typedef boost::asio::ip::tcp::resolver::query CQuery;
typedef boost::asio::io_service CIoSrv;
typedef boost::asio::ip::tcp::resolver CResolver;
typedef boost::asio::ip::tcp::resolver::iterator CIterator;
typedef boost::asio::ip::tcp::endpoint CEndPt;
void main()
{
CQuery oQuery("www.boost.org", "http");//表示一个服务器的地址和服务类型
cout << "HostName : " << oQuery.host_name() << endl;
cout << "ServiceName : " << oQuery.service_name() << endl;
CIoSrv oIoSrv;//IO服务对象来执行一个IO上的操作
CResolver oResolver(oIoSrv);//来表示一个域名和服务类型的解释操作
//进行域名或者服务解释,以便生成SOCKET内部使用的数据格式
CIterator itBegin = oResolver.resolve(oQuery), itEnd;
if(itBegin == itEnd) return;
//解释成功之后, 显示解释后的IP地址和端口
CEndPt oEndPt = itBegin->endpoint();
cout << "HostIp : " << oEndPt.address() << endl;
cout << "PortSrv : " << oEndPt.port() << endl;
}