Poco::Net::DNS を紹介します。
DNSTest.cpp
・Poco::Net::DNS::hostName() で自身のホスト名を取得し表示。
・Poco::Net::DNS::flushCache() でキャッシュをクリアした後、
- Poco::Net::DNS::hostByName()
- Poco::Net::DNS::hostByAddress()
- Poco::Net::DNS::resolve(hostname)
- Poco::Net::DNS::resolve(hostaddress)
で取得した Poco::Net::HostEntry の要素を列挙。
#include <Poco/Logger.h> #include <Poco/PatternFormatter.h> #include <Poco/FormattingChannel.h> #include <Poco/ConsoleChannel.h> #include <Poco/Net/DNS.h> #include <Poco/Net/IPAddress.h> #include <Poco/Net/NetException.h> #include <string> #include "ScopedLogMessage.h" void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION) { Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t")); pFCConsole->setChannel(new Poco::ConsoleChannel); pFCConsole->open(); Poco::Logger::create(name, pFCConsole, level); } void ShowHostEntryItems(const Poco::Net::HostEntry& he, const ScopedLogMessage& msg) { msg.Message(Poco::format(" name: %s", he.name())); for(Poco::Net::HostEntry::AddressList::const_iterator itr=he.addresses().begin(); itr!=he.addresses().end(); ++itr) { msg.Message(Poco::format(" address#%d: %s", static_cast<int>(itr-he.addresses().begin()), itr->toString())); } for(Poco::Net::HostEntry::AliasList::const_iterator itr=he.aliases().begin(); itr!=he.aliases().end(); ++itr) { msg.Message(Poco::format(" alias#%d: %s", static_cast<int>(itr-he.aliases().begin()), *itr)); } } int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("DNSTest ", "start", "end"); msg.Message("Poco::Net::DNS::hostName()"); msg.Message(Poco::format(" %s", Poco::Net::DNS::hostName())); const std::string hostname("www.google.com"); const std::string hostaddress("66.249.89.104"); const Poco::Net::IPAddress ipaddress(hostaddress); Poco::Net::DNS::flushCache(); try { msg.Message(Poco::format("Poco::Net::DNS::hostByName(\"%s\")", hostname)); ShowHostEntryItems(Poco::Net::DNS::hostByName(hostname), msg); msg.Message(Poco::format("Poco::Net::DNS::hostByAddress(Poco::Net::IPAddress(\"%s\"))", ipaddress.toString())); ShowHostEntryItems(Poco::Net::DNS::hostByAddress(ipaddress), msg); msg.Message(Poco::format("Poco::Net::DNS::resolve(\"%s\")", hostname)); ShowHostEntryItems(Poco::Net::DNS::resolve(hostname), msg); msg.Message(Poco::format("Poco::Net::DNS::resolve(\"%s\")", hostaddress)); ShowHostEntryItems(Poco::Net::DNS::resolve(hostaddress), msg); } catch(Poco::Net::HostNotFoundException&) { } catch(Poco::Net::NoAddressFoundException&) { } catch(Poco::Net::DNSException&) { } catch(Poco::IOException&) { } return 0; }
Results of execution
・On Linux
[0] DNSTest start [0] Poco::Net::DNS::hostName() [0] debian [0] Poco::Net::DNS::hostByName("www.google.com") [0] name: www.l.google.com [0] address#0: 66.249.89.104 [0] address#1: 66.249.89.99 [0] alias#0: www.google.com [0] Poco::Net::DNS::hostByAddress(Poco::Net::IPAddress("66.249.89.104")) [0] name: www.l.google.com [0] address#0: 66.249.89.104 [0] address#1: 66.249.89.99 [0] alias#0: www.google.com [0] Poco::Net::DNS::resolve("www.google.com") [0] name: www.l.google.com [0] address#0: 66.249.89.104 [0] address#1: 66.249.89.99 [0] alias#0: www.google.com [0] Poco::Net::DNS::resolve("66.249.89.104") [0] name: www.l.google.com [0] address#0: 66.249.89.104 [0] address#1: 66.249.89.99 [0] alias#0: www.google.com [0] DNSTest end
・On Macintosh OSX 10.6.6
[0] DNSTest start [0] Poco::Net::DNS::hostName() [0] Mac-mini.local [0] Poco::Net::DNS::hostByName("www.google.com") [0] name: www.l.google.com [0] address#0: 66.249.89.99 [0] address#1: 66.249.89.104 [0] alias#0: www.google.com [0] Poco::Net::DNS::hostByAddress(Poco::Net::IPAddress("66.249.89.104")) [0] name: nrt04s01-in-f104.1e100.net [0] address#0: 66.249.89.104 [0] alias#0: 104.89.249.66.in-addr.arpa [0] Poco::Net::DNS::resolve("www.google.com") [0] name: www.l.google.com [0] address#0: 66.249.89.99 [0] address#1: 66.249.89.104 [0] alias#0: www.google.com [0] Poco::Net::DNS::resolve("66.249.89.104") [0] name: nrt04s01-in-f104.1e100.net [0] address#0: 66.249.89.104 [0] alias#0: 104.89.249.66.in-addr.arpa [0] DNSTest end
・On Windows XP sp3
[0] DNSTest start [0] Poco::Net::DNS::hostName() [0] setsu-shy3m0tg8 [0] Poco::Net::DNS::hostByName("www.google.com") [0] name: www.google.com [0] address#0: 66.249.89.99 [0] Poco::Net::DNS::hostByAddress(Poco::Net::IPAddress("66.249.89.104")) [0] name: nrt04s01-in-f104.1e100.net [0] address#0: 66.249.89.104 [0] Poco::Net::DNS::resolve("www.google.com") [0] name: www.google.com [0] address#0: 66.249.89.99 [0] Poco::Net::DNS::resolve("66.249.89.104") [0] name: nrt04s01-in-f104.1e100.net [0] address#0: 66.249.89.104 [0] DNSTest end
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
・2011年3月13日からのダウンロード数:135
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
| Copyright © 2011 Round Square Inc. All rights reserved. |
|---|