C + + Boost::asio programming-domain name resolution detailed introduction

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.