手机
当前位置:查字典教程网 >编程开发 >C语言 >C++ boost::asio编程-域名解析详细介绍
C++ boost::asio编程-域名解析详细介绍
摘要:C++boost::asio编程-域名解析在网络通信中通常我们并不直接使用IP地址,而是使用域名。这时候我们就需要用reslover类来通过...

C++ boost::asio编程-域名解析

在网络通信中通常我们并不直接使用IP地址,而是使用域名。这时候我们就需要用reslover类来通过域名获取IP,它可以实现

与IP版本无关的网址解析。

#include "stdafx.h" #include "boost/asio.hpp" #include "boost/shared_ptr.hpp" #include "boost/thread.hpp" #include <boost/lexical_cast.hpp>//使用字符串转换功能 using namespace std; using namespace boost::asio; #ifdef _MSC_VER #define _WIN32_WINNT 0X0501 //避免VC下编译警告 #endif //域名解析为IP //入参:域名,端口 //返回:ip地址 vector<string> domain2ip(const char *domain,int port) { io_service ios; //创建resolver对象 ip::tcp::resolver slv(ios); //创建query对象 ip::tcp::resolver::query qry(domain,boost::lexical_cast<string>(port));//将int型端口转换为字符串 //使用resolve迭代端点 ip::tcp::resolver::iterator it=slv.resolve(qry); ip::tcp::resolver::iterator end; vector<string> ip; for(;it!=end;it++) { ip.push_back((*it).endpoint().address().to_string()); } return ip; } int _tmain(int argc, _TCHAR* argv[]) { vector<string> ip=domain2ip("www.csdn.net",0); for(int i=0;i<ip.size();i++) { cout<<ip[i]<<endl; } getchar(); return 0; }

其中经过测试,端口可以填任意值均可以解析出来。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

【C++ boost::asio编程-域名解析详细介绍】相关文章:

c++中的消息框messagebox()详细介绍及使用方法

解析wprintf 中使用%I64d格式化输出LONGLONG的详细介绍

iostream与iostream.h的区别详细解析

C++中delete和delete[]的区别详细介绍

基于C++ list中erase与remove函数的使用详解

C++ using namespace std 用法深入解析

C++空类默认函数详细解析

C++中virtual继承的深入理解

C/C++中static,const,inline三种关键字详细总结

C++中this指针的用法及介绍

精品推荐
分类导航