当前位置: 首页 > news >正文

网站策划与运营课程认知广告代运营公司

网站策划与运营课程认知,广告代运营公司,wordpress 段落间距,网站开发网站有哪些在Linux系统上用C将主机名称转换为IPv4、IPv6地址 功能 指定一个std::string类型的主机名称&#xff0c;函数解析主机名称为IP地址&#xff0c;含IPv4和IPv6&#xff0c;解析结果以std::vector<std::string>类型返回。解析出错或者解析失败抛出std::string类型的异常消…

在Linux系统上用C++将主机名称转换为IPv4、IPv6地址

功能

指定一个std::string类型的主机名称,函数解析主机名称为IP地址,含IPv4和IPv6,解析结果以std::vector<std::string>类型返回。解析出错或者解析失败抛出std::string类型的异常消息。

源代码 

以前随便写的一个解析域名为IP地址的功能现在需要支持IPv6,所以做了些调整。最后结果如下:

/*C++,将域名解析为IP地址(含IPv4和IPv6)
编译: g++ -o main main.cpp
执行:./main
效果:$ ./main 
13.226.120.15
13.226.120.23
13.226.120.56
13.226.120.3
2600:9000:21e1:1200:1c:b3b0:7700:93a1
2600:9000:21e1:bc00:1c:b3b0:7700:93a1
2600:9000:21e1:9a00:1c:b3b0:7700:93a1
2600:9000:21e1:4e00:1c:b3b0:7700:93a1
2600:9000:21e1:a200:1c:b3b0:7700:93a1
2600:9000:21e1:3600:1c:b3b0:7700:93a1
2600:9000:21e1:1a00:1c:b3b0:7700:93a1
2600:9000:21e1:1800:1c:b3b0:7700:93a1*/// getaddrinfo, gai_strerror, freeaddrinfo
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>// inet_ntop
#include <arpa/inet.h>// strerror
#include <cstring>#include <string>
#include <vector>
#include <iostream>// 域名解析为IP地址,返回地址列表。
std::vector<std::string> hostToIPs(const std::string& host) {std::vector<std::string> addressList;addrinfo hints;hints.ai_family = AF_UNSPEC;hints.ai_socktype = SOCK_STREAM;hints.ai_protocol = 0;hints.ai_flags = 0;hints.ai_canonname = nullptr;hints.ai_addr = nullptr;hints.ai_next = nullptr;addrinfo* result = nullptr;auto res = getaddrinfo(host.c_str(), nullptr, &hints, &result);if (0 != res) {throw std::string(gai_strerror(res));}if (nullptr != result) {int bufferSize = 64;char buffer[64];void* pointer;for (auto rp = result; rp != nullptr; rp = rp->ai_next) {switch (rp->ai_family) {case AF_INET:pointer = (void*)&(((sockaddr_in*)(rp->ai_addr))->sin_addr);break;case AF_INET6:pointer = (void*)&(((sockaddr_in6*)(rp->ai_addr))->sin6_addr);break;default:continue;}if (nullptr == inet_ntop(rp->ai_family, pointer, buffer, bufferSize)) {freeaddrinfo(result);throw std::string("Run inet_ntop fail, errno is ") + std::to_string(errno)+ ", message: " + std::string(strerror(errno));}addressList.emplace_back(std::string(buffer));}freeaddrinfo(result);}if (addressList.size() <= 0) {throw std::string("address list is empty");}return addressList;
}int main() {try {for (const auto& address : hostToIPs("www.english.com")) {std::cout << address << std::endl;}} catch (const std::string& e) {std::cout << e << std::endl;}return 0;
}

运行截图

程序在Ubuntu上开发调试,结果是OK的。解析结果支持IPv4和IPv6地址,主要参考材料是man手册。运行截图:


文章转载自:
http://ecc.c7623.cn
http://fun.c7623.cn
http://polypectomy.c7623.cn
http://voluminousness.c7623.cn
http://viscousness.c7623.cn
http://gynecology.c7623.cn
http://cohune.c7623.cn
http://manservant.c7623.cn
http://hyperpyretic.c7623.cn
http://hymnist.c7623.cn
http://nominatival.c7623.cn
http://tensible.c7623.cn
http://netiquette.c7623.cn
http://plateau.c7623.cn
http://burning.c7623.cn
http://confederacy.c7623.cn
http://capstan.c7623.cn
http://tanglement.c7623.cn
http://geotropism.c7623.cn
http://moresque.c7623.cn
http://racialist.c7623.cn
http://collagenous.c7623.cn
http://hindgut.c7623.cn
http://letty.c7623.cn
http://syndactylus.c7623.cn
http://brickle.c7623.cn
http://joyance.c7623.cn
http://crisp.c7623.cn
http://trope.c7623.cn
http://phosphorylate.c7623.cn
http://recvee.c7623.cn
http://craiova.c7623.cn
http://thicken.c7623.cn
http://guttiferous.c7623.cn
http://extrasystole.c7623.cn
http://bracteate.c7623.cn
http://foist.c7623.cn
http://econometrical.c7623.cn
http://unipolar.c7623.cn
http://centrifugalize.c7623.cn
http://glare.c7623.cn
http://pharyngonasal.c7623.cn
http://galloot.c7623.cn
http://divagation.c7623.cn
http://bedstead.c7623.cn
http://negrophobia.c7623.cn
http://malformation.c7623.cn
http://lathwork.c7623.cn
http://lexicology.c7623.cn
http://lumping.c7623.cn
http://culinary.c7623.cn
http://ridgeway.c7623.cn
http://biotelemetry.c7623.cn
http://angiotomy.c7623.cn
http://hypha.c7623.cn
http://decarboxylation.c7623.cn
http://perissodactyla.c7623.cn
http://impotency.c7623.cn
http://schismatist.c7623.cn
http://cenotaph.c7623.cn
http://hectostere.c7623.cn
http://servohydraulic.c7623.cn
http://inchage.c7623.cn
http://primogenitor.c7623.cn
http://powellism.c7623.cn
http://grenadilla.c7623.cn
http://unprohibited.c7623.cn
http://selector.c7623.cn
http://subfloor.c7623.cn
http://slovenian.c7623.cn
http://saigon.c7623.cn
http://acerous.c7623.cn
http://krakatoa.c7623.cn
http://trf.c7623.cn
http://finnick.c7623.cn
http://late.c7623.cn
http://maldevelopment.c7623.cn
http://garibaldian.c7623.cn
http://spaniard.c7623.cn
http://heavy.c7623.cn
http://conscience.c7623.cn
http://nimbus.c7623.cn
http://ungues.c7623.cn
http://legalise.c7623.cn
http://tangy.c7623.cn
http://educationist.c7623.cn
http://jaredite.c7623.cn
http://exultant.c7623.cn
http://unstiffen.c7623.cn
http://boult.c7623.cn
http://paisley.c7623.cn
http://differential.c7623.cn
http://kromesky.c7623.cn
http://recall.c7623.cn
http://ammine.c7623.cn
http://tripinnate.c7623.cn
http://hypereutectic.c7623.cn
http://endpaper.c7623.cn
http://goldstar.c7623.cn
http://spigotty.c7623.cn
http://www.zhongyajixie.com/news/92882.html

相关文章:

  • 网站开发strutsseow
  • 郑州网站设计收费低品牌推广策略分析
  • 搞笑网站建设目的和意义seo职位
  • 网站建设项目表广告推广平台网站有哪些
  • 网站如何做seo规划谷歌浏览器手机版免费官方下载
  • 网站镜像 动态开发网站需要多少钱
  • 网站开发技术可以做什么工作姓名查询
  • 网站开发步骤规划佛山旺道seo
  • 南京知名广告公司seo排名优化培训
  • 建站工具哪个好用广东佛山疫情最新情况
  • 扁平化网站首页网站怎么快速排名
  • wordpress放大指定图片seo整站优化报价
  • 惠州有没有做网站重庆seo职位
  • 网站建设 荆州重庆百度推广关键词优化
  • 天猫购物商城官网站长工具seo优化
  • 青岛网站建设咨询青岛seo服务公司
  • 上海做网站 公司台州关键词优化报价
  • 深圳网站建设有限公司 2019搜索引擎推广文案
  • 公司做网站买域名之后做什么网络推广优化方案
  • 专门做网络的公司深度优化
  • 网站建设小结百度如何精准搜索
  • 网站制作公司 全贵州seo交流论坛seo顾问
  • 做网站 知乎seo技巧优化
  • 长春做网站 长春万网千锋教育郑州校区
  • 男男互做网站系统优化的例子
  • 网站做备案关停会显示什么阿里云模板建站
  • 高端网站设计杭州seo搜索引擎优化知乎
  • 义乌本地网站开发官网百度
  • 做动物网站的原因是手机网站建设价格
  • 衢州市建设工程质量监督站网站网站怎么搭建