简体   繁体   中英

How to catch boost asio boost::system::error_code connect exception separatly from other exceptions?

Boost asio has such socket connect api . I have such simple code:

try
{
    std::string addr;
    std::string port;
    sscanf(tcpUrl.c_str(), "tcp://%[^:]:%d", &addr, &port);

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), addr.c_str(), port.c_str());
    tcp::resolver::iterator iterator = resolver.resolve(query);

    tcp::socket s(io_service);
    s.connect(*iterator);
    Sleep(250);
    s.close();
}
catch (std::exception& e)
{
    return -1;
}

All I want is to try to connect and catch boost::system::error_code connect exception. Only it. I do not need to cach any more. and on it I need to return -1. How to do such thing?

catch( const boost::system::system_error& ex )
{
  return -1;
}

Please catch by const reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM