如何捕获 boost asio boost::system::error_code 连接异常与其他异常?
Boost asio有这样的 socket connect api 。我有这样简单的代码:
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;
}
我想要的只是尝试连接并捕获 boost::system::error_code 连接异常。只有它。我不需要再缓存了。我需要返回-1。这样的事该怎么办呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请通过常量引用来捕获。
Please catch by const reference.