Poco library Chinese programming Reference Guide (9) poco: Net: DNS

Source: Internet
Author: User


Poco library Chinese programming Reference Guide (9) poco: Net: DNS

  • Author: Liu Da-poechant (Zhong Chao)
  • Email: zhongchao.ustc@gmail.com
  • Blog: blog.csdn.net/poechant
  • Date: Limit l 20Th, 2012
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 stores host Primary name (canonical name), alias name list, and IP address list.

2 related APIs

Get a hostentry instance:

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

Output Domain Name:

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

Address and alias:

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

The sample in the official poco documentation is incorrect. The following provides a correct example, where the domain name is passed in through parameters.

#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;}

Compile:

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

Run:

$ ./name_solver baidu.comCanonical Name: baidu.comAddress: 123.125.114.144Address: 123.125.114.144Address: 220.181.111.85Address: 220.181.111.85Address: 220.181.111.86Address: 220.181.111.86$ ./name_solver www.ustc.edu.cnCanonical Name: ustc.edu.cnAddress: 202.38.64.246Address: 202.38.64.246

-

Reprinted, please indicate the csdn blog from LIU Da: blog.csdn.net/poechant

-

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.