1 poco::net::D NS
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 ();}
;
}
Host primary name (canonical name), alias Name list, IP address list are stored in hostentry.
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
POCO's official documentation has an error with sample, the following provides a correct example where the domain name is passed in by parameter.
#include "poco/net/dns.h" #include <iostream> using poco::net::D ns;
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.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 from Liuda: blog.csdn.net/poechant
View a full set of articles: Http://www.bianceng.cn/Programming/cplus/201301/35022.htm