[Boost-users] boost::asio::ip::tcp::resolver::query does not work in Ubuntu!!!

989 views
Skip to first unread message

mozdemir

unread,
May 11, 2012, 11:54:19 PM5/11/12
to boost...@lists.boost.org
Hi,

I am a newbie with Asio library and i got into crashing problem with the
following code:


boost::system::error_code ec;
boost::asio::ip::tcp::resolver resolver( m_hive->GetService() );
boost::asio::ip::tcp::resolver::query query( m_host,
boost::lexical_cast< std::string >( m_port ));
boost::asio::ip::tcp::resolver::iterator endpointIterator =
resolver.resolve( query );

When i run this in windows, i do not have any isssue but in Ubuntu, it works
and after some more resolution of query, it crashes. I did install Miredo to
activate Ipv6 but it did not help.

When the crash happens, i see this:


23:37.47 [dbg] [0x8ec0650] Exception: Host not found (authoritative)
23:37.47 [dbg] [0x8ec0650] Exception: Host not found (authoritative)
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector&lt;boost::system::system_error>
>'
what(): Host not found (authoritative)
Aborted (core dumped)


Do you know what else is missing or what is wrong?

Thanks a lot,
mustafa

--
View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-ip-tcp-resolver-query-does-not-work-in-Ubuntu-tp4628195.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Andreas Wehrmann

unread,
May 12, 2012, 3:50:50 AM5/12/12
to boost...@lists.boost.org
> Hi,
>
> I am a newbie with Asio library and i got into crashing problem with the
> following code:
>
>
> boost::system::error_code ec;
> boost::asio::ip::tcp::resolver resolver( m_hive->GetService() );
> boost::asio::ip::tcp::resolver::query query( m_host,
> boost::lexical_cast< std::string >( m_port ));
> boost::asio::ip::tcp::resolver::iterator endpointIterator =
> resolver.resolve( query );
>
> When i run this in windows, i do not have any isssue but in Ubuntu, it works
> and after some more resolution of query, it crashes. I did install Miredo to
> activate Ipv6 but it did not help.
>
> When the crash happens, i see this:
>
>
> 23:37.47 [dbg] [0x8ec0650] Exception: Host not found (authoritative)
> 23:37.47 [dbg] [0x8ec0650] Exception: Host not found (authoritative)
> terminate called after throwing an instance of
> 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector&lt;boost::system::system_error>
>> '
> what(): Host not found (authoritative)
> Aborted (core dumped)
>
>
> Do you know what else is missing or what is wrong?
>
> Thanks a lot,
> mustafa
>

Provided your output, this is not a crash but just the case of an
unhandled exception.
Take a look at this:

http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference/ip__basic_resolver/resolve/overload1.html

It says that resolve() throws an exception on error
so you can either catch it or use the overload where you need
to provide an error handle yourself (which is not much different
since you need to handle errors anyway).

Regards,
Andreas

mozdemir

unread,
May 12, 2012, 1:27:16 PM5/12/12
to boost...@lists.boost.org
HI Andreas,

you are right. this is not crash. It is an exception.

I did the following small code to explain the issue better

============================================

#include <iostream>
#include <boost/asio/ip/tcp.hpp>
#include <boost/lexical_cast.hpp>


using namespace boost::asio;

int main(int argc, char* argv[])
{
io_service ios;
try {
int i;
for (i=0;i<1000;i++) {
int m_port = 80;
std::string m_host = "www.yahoo.com";
boost::system::error_code ec;
boost::asio::ip::tcp::resolver resolver( ios );
boost::asio::ip::tcp::resolver::query query( m_host,
boost::lexical_cast< std::string >( m_port ));

boost::asio::ip::tcp::resolver::iterator endpointIterator =
resolver.resolve( query );
for(;endpointIterator != ip::tcp::resolver_iterator();
++endpointIterator) {
std::cout << "i: " << i << " ip: " <<
endpointIterator->endpoint().address().to_string() << std::endl;
}
}

} catch(std::exception const& ex) {
std::cerr << "Error: " << ex.what() << '\n';
}
}
=============================================

the output goes fine but around 300, it throws the exception.

i: 327 ip: 98.139.183.24
i: 328 ip: 98.139.183.24
i: 329 ip: 98.139.183.24

Error: Host not found (authoritative)

============================================

you are suggesting to handle the exception which I will do. But i wonder why
this is not happening in windows but ubuntu?

I wonder if my ubuntu is missing some configuration. I did install Miredo
for Ipv6 but it did not help.
i feel like this is something to do with network settings issue, NAT issue
or something....

Thank you for the suggestion.
mustafa


--
View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-ip-tcp-resolver-query-does-not-work-in-Ubuntu-tp4628195p4629193.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Andreas Wehrmann

unread,
May 12, 2012, 4:53:56 PM5/12/12
to boost...@lists.boost.org

Zitat von mozdemir <mozde...@yahoo.com>:
I'm not sure right now whether it is against the policy of this mailing list,
but take a look at this:

http://stackoverflow.com/questions/4956915/how-can-i-adapt-this-c-testcase-so-it-continues-to-work-with-a-new-network-dev

This looks similar to your example.
Let us know if it helped.

Regards,
Andreas

mozdemir

unread,
May 12, 2012, 10:39:39 PM5/12/12
to boost...@lists.boost.org


Hi Andreas,

I have changed the code like below in the light of the link you sent.
it did help for sure. The stats comparing both case are:

1. Without res_init(), the frequency is around 6 exceptions after 2047
resolutions:

totalRes: 2047 errorNum: 6 Error: Host not found (authoritative)

2. with res_init(), in the first run i saw 2 exceptions out of 2000 but
later on i run it long time and did not see any exceptions!

Hopefully, this is the right fix. I leave the code below to help others.

Thanks a lot,
mustafa

================================================

#include <iostream>
#include <boost/asio/ip/tcp.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <resolv.h>


using namespace boost::asio;

int main(int argc, char* argv[])
{
io_service ios;
int errorNum = 0;
int totalRes = 0;
while (true) {
try {
int i;
for (i=0;i<1000;i++) {
res_init();
int m_port = 80;
std::string m_host = "www.yahoo.com";
boost::system::error_code ec;
boost::asio::ip::tcp::resolver resolver( ios );
boost::asio::ip::tcp::resolver::query query( m_host,
boost::lexical_cast< std::string >( m_port ));

boost::asio::ip::tcp::resolver::iterator endpointIterator =
resolver.resolve( query );
for (;endpointIterator != ip::tcp::resolver_iterator();
++endpointIterator) {
std::cout << "i: " << i << " ip: " <<
endpointIterator->endpoint().address().to_string() << std::endl;
totalRes++;
}
boost::this_thread::sleep(boost::posix_time::milliseconds(
100 ));
}

} catch (std::exception const& ex) {
errorNum++;
std::cerr << "totalRes: " << totalRes << " errorNum: " <<
errorNum <<" Error: " << ex.what() << '\n';
boost::this_thread::sleep(boost::posix_time::milliseconds( 3000
));
}
}

return 0;
}
========================================================

--
View this message in context: http://boost.2283326.n4.nabble.com/boost-asio-ip-tcp-resolver-query-does-not-work-in-Ubuntu-tp4628195p4629904.html
Sent from the Boost - Users mailing list archive at Nabble.com.
Reply all
Reply to author
Forward
0 new messages