Boost logo

Boost Users :

Subject: [Boost-users] [ASIO][Windows] async_resolve() Blocking on Reverse Lookup?
From: Kyle Ketterer (reminisc3_at_[hidden])
Date: 2014-12-06 15:31:44


Hello,

I am working on a simple utility which scans an IP range and performs a
reverse lookup. Using ASIO, I would like to use async_resolve to accomplish
this. However, it seems that when using async_resolve() with an ::endpoint
overload, the call is blocking.

I am wrapping all async functions in a strand and calling io_service::run()
from multiple threads.

Here is an example:

-------------------------------------------------------------------

class client
{

public:

    client ( boost::asio::io_service & io_service ) :
io_service_(io_service), resolver_(io_service), strand_(io_service)
    {

        /* */

    }

    void resolve_host ( const std::string & host )
    {

        strand_.post( boost::bind(&client::start_resolve, this, host ) );

    }

    void start_resolve ( const std::string & host_ )
    {

        //boost::asio::ip::tcp::resolver::query query( tcp::v4(), host_, ""
);

        tcp::endpoint endpoint ( boost::asio::ip::address::from_string(
host_) , 0 );

        resolver_.async_resolve( endpoint, strand_.wrap( boost::bind(
&client::handle_resolve, this, boost::asio::placeholders::error,
boost::asio::placeholders::iterator ) ) );

    }

    void handle_resolve ( const boost::system::error_code& err,
tcp::resolver::iterator endpoint_iterator )
    {

        if ( !err )
        {

            /* attempt to connect */
            tcp::endpoint endpoint = *endpoint_iterator;

            cout << endpoint.address() << "' >> " <<
(*endpoint_iterator).host_name() << endl;

        }

    }

private:

    tcp::resolver resolver_;
    boost::asio::strand strand_;
    boost::asio::io_service & io_service_;

};

int main( int argc, char* argv[] )
{

    /* io service */
    boost::asio::io_service io_service;

    /* ip range */
    std::string m_addr = "10.1.10.";

    /* client object */
    boost::shared_ptr<client> m_client ( new client ( io_service ) );

    /* add the entire subnet */
    for ( int i=1; i<255; i++ )
    {

        m_client->resolve_host ( m_addr + boost::to_string(i) );

    }

   /* create threads and run io_service */

    std::vector < boost::shared_ptr<boost::thread> > m_threads;

    for ( int i=0; i < CPU_CORES; i++ )
    {
        m_threads.push_back ( boost::shared_ptr<boost::thread> ( new
boost::thread(boost::bind(&boost::asio::io_service::run, &io_service ) ) )
);
    }

    for ( int i=0; i < CPU_CORES; i++ )
    {
        m_threads[i]->join();
    }

    return 0;

}

------------------------------------------------------

I am looking for suggestions on how I can take a multi-threaded approach to
this. I am fairly new to ASIO so I appreciate any advice!

Thanks

-- 
Kyle Ketterer
<reminisc3_at_[hidden]>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net