C + + Boost::asio programming-domain name resolution
In network communication, we usually do not use the IP address directly, but instead use the domain name. At this time we need to use the Reslover class to obtain IP through the domain name, it can achieve
URL resolution unrelated to the IP version.
#include "stdafx.h" #include "boost/asio.hpp" #include "boost/shared_ptr.hpp" #include "boost/thread.hpp" #include < boost/lexical_cast.hpp>//Use the string conversion function using namespace std; using namespace Boost::asio; #ifdef _msc_ver #define _WIN32_WINNT 0x0501//Avoid VC compilation warning #endif//Domain name resolution to IP//entry: Domain name, port//return: IP address vector<string> dom AIN2IP (const char *domain,int port) {Io_service iOS; Create resolver Object Ip::tcp::resolver SLV (iOS); Create a Query Object Ip::tcp::resolver::query qry (domain,boost::lexical_cast<string> (port));//convert int port to string// Use resolve Iteration Endpoint 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; }
After testing, the port can be filled with any value can be parsed out.
Thank you for reading, hope to help everyone, more relevant articles please pay attention to topic.alibabacloud.com (www.php.cn)!