程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++ POCO庫中文編程參考指南(9) Poco::Net::DNS

C++ POCO庫中文編程參考指南(9) Poco::Net::DNS

編輯:關於C++

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved