欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

C語言

C++ boost::asio編程-域名解析詳解

時(shí)間:2025-05-19 18:55:26 C語言 我要投稿
  • 相關(guān)推薦

C++ boost::asio編程-域名解析詳解

  在網(wǎng)絡(luò)通信中通常我們并不直接使用IP地址,而是使用域名。這時(shí)候我們就需要用reslover類來通過域名獲取IP,它可以實(shí)現(xiàn)與IP版本無關(guān)的網(wǎng)址解析。下面,就和小編一起來看一看C++ boost::asio編程-域名解析詳解,希望對大家有幫助!


  #include "stdafx.h"

  #include "boost/asio.hpp"

  #include "boost/shared_ptr.hpp"

  #include "boost/thread.hpp"

  #include <boost/lexical_cast.hpp>//使用字符串轉(zhuǎn)換功能

  using namespace std;

  using namespace boost::asio;

  #ifdef _MSC_VER

  #define _WIN32_WINNT  0X0501 //避免VC下編譯警告

  #endif

  //域名解析為IP

  //入?yún)ⅲ河蛎,端?/p>

  //返回:ip地址

  vector<string> domain2ip(const char *domain,int port)

  {

  io_service ios;

  //創(chuàng)建resolver對象

  ip::tcp::resolver slv(ios);

  //創(chuàng)建query對象

  ip::tcp::resolver::query qry(domain,boost::lexical_cast<string>(port));//將int型端口轉(zhuǎn)換為字符串

  //使用resolve迭代端點(diǎn)

  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;

  }

  get);

  return 0;

  }

  其中經(jīng)過測試,端口可以填任意值均可以解析出來。


【C++ boost::asio編程-域名解析詳解】相關(guān)文章:

C++ this指針詳解07-04

c++快速排序詳解10-18

c++ 中--declspec 的用法詳解08-13

C++ cin輸入流詳解10-11

C++ 排序插入排序詳解08-03

C++類中的繼承實(shí)例詳解07-05

C++冒泡排序算法實(shí)例詳解06-09

C++的字符串分割函數(shù)的使用詳解08-08

c++運(yùn)算符重載基礎(chǔ)知識詳解08-20