DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

1 Poco::Net::DNS

namespace Poco {
namespace Net {
class Net_API DNS {
public:
    static HostEntry hostByName(const std::string& hostname);
    static HostEntry hostByAddress(const IPAddress& address);
    static HostEntry resolve(const std::string& address);
    static IPAddress resolveOne(const std::string& address);
    static HostEntry thisHost();
    static std::string hostName();
};
}
}

HostEntry 中存储 host primary name(canonical name)、alias name list、IP address list。

2 相关 API

得到一个 HostEntry 实例:

const HostEntry& entry = DNS::hostByName("google.com");

输出域名:

std::cout << entry.name() << std::endl;

Address 和 Alias:

const HostEntry::AliasList& aliases = entry.aliases();
const HostEntry::AddressList& addrs = entry.addresses();

3 Source code

POCO 的官方文档中的 Sample 有错误,以下提供一个正确的示例,其中域名是通过参数传入的。

#include "Poco/Net/DNS.h"
#include <iostream>
using Poco::Net::DNS;
using Poco::Net::IPAddress;
using Poco::Net::HostEntry;
int main(int argc, char** argv)
{
    if (argc != 2) {
        std::cout << "Invalid argument number." << std::endl;
    }
    const HostEntry& entry = DNS::hostByName(argv[1]);
    std::cout << "Canonical Name: " << entry.name() << std::endl;

    const HostEntry::AliasList& aliases = entry.aliases();
    for (HostEntry::AliasList::const_iterator it = aliases.begin();
        it !=   aliases.end(); ++it)
        std::cout << "Alias: " << *it << std::endl;

    const HostEntry::AddressList& addrs = entry.addresses();
    for (HostEntry::AddressList::const_iterator it = addrs.begin();
        it !=   addrs.end(); ++it)
        std::cout << "Address: " << it->toString() << std::endl;

    return 0;
}

编译:

$ g++ name_solver.cpp -o name_solver \
-I/usr/local/include -I/usr/local/lib -lPocoNet

运行:

$ ./name_solver baidu.com
Canonical Name: baidu.com
Address: 123.125.114.144
Address: 123.125.114.144
Address: 220.181.111.85
Address: 220.181.111.85
Address: 220.181.111.86
Address: 220.181.111.86

$ ./name_solver www.ustc.edu.cn
Canonical Name: ustc.edu.cn
Address: 202.38.64.246
Address: 202.38.64.246

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

posted on   DoubleLi  阅读(1151)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2013-09-29 C/C++代码静态检查工具Cppcheck在VS2008开发环境中的安装配置和使用
2013-09-29 MFC上下浮动与渐入渐出消息提示框实现
2013-09-29 MFC渐入渐出框实现方式二
2013-09-29 Cppcheck软件使用
点击右上角即可分享
微信分享提示