|
Boost : |
Subject: Re: [boost] boost::asio and resolve_async
From: Kevin Heifner (heifnerk_at_[hidden])
Date: 2012-08-16 11:18:50
On 8/16/2012 9:35 AM, david.hagood_at_[hidden] wrote:
> I was looking at needing to do a large number of reverse DNS lookups for a
> log analysis program I am writing, and wanted to use some form of async
> lookup to speed the program up (vs. doing each look up in series, at a
> couple of seconds per lookup).
Note: "An additional thread per io_service is used to emulate
asynchronous host resolution. This thread is created on the first call
to either ip::tcp::resolver::async_resolve() or
ip::udp::resolver::async_resolve()." from
http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/overview/implementation.html
> boost::asio and the async_resolve functions looked to be just what I
> wanted, BUT: I can find no good examples of a simple lookup. By "simple" I
> mean "given an address, give me the host name(s) associated with that
> address". All the examples were for a lookup by host name/transport
> type/port, e.g. host name/tcp/80. In my case I neither need a port # nor
> even have one - I just want the RDNS for the address.
namespace {
void handle_resolve(const boost::system::error_code& ec,
boost::asio::ip::tcp::resolver::iterator ep_it) {
if (!ec) {
// print ip addresses
for (boost::asio::ip::tcp::resolver::iterator end; ep_it != end;
++ep_it) {
std::cout << ep_it->host_name() << ": " <<
ep_it->endpoint().address().to_string() << std::endl;
}
} else {
std::cerr << "Resolve error: " << ec << std::endl;
}
}
}
BOOST_AUTO_TEST_CASE(test_async_resolve)
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query1("www.google.com", "");
boost::asio::ip::tcp::resolver::query query2("www.yahoo.com", "");
boost::asio::ip::tcp::resolver::query query3("www.boost.org", "");
boost::asio::ip::tcp::resolver::query query4("www.ociweb.com", "");
resolver.async_resolve(query1, boost::bind(&handle_resolve, _1, _2));
resolver.async_resolve(query2, boost::bind(&handle_resolve, _1, _2));
resolver.async_resolve(query3, boost::bind(&handle_resolve, _1, _2));
resolver.async_resolve(query4, boost::bind(&handle_resolve, _1, _2));
io_service.run();
}
KevinH
-- Kevin Heifner heifnerk_at_[hidden] Object Computing, Inc. www.ociweb.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk