qua*_*ell 6 c++ linux boost-asio
io_service:run在Linux上发出异常.
这就是发生的事情.我使用Boost.Asio实现了简单的异步echo服务器.它是单线程的,一切都是异步的,也就是说我只使用了accept,send和receive函数的异步版本.当客户端没有正常断开连接(例如它崩溃)时,服务器的事件循环抛出boost :: system :: system_error异常remote_endpoint:传输端点未连接.为什么会发生如何应对呢?它是由SIGPIPE信号引起的吗?如果是这样,保持服务器运行的最佳方法是什么?处理异常或处理信号?
该异常表示basic_stream_socket::remote_endpoint()调用了抛出版本,其中在getpeername()返回的底层调用中出现错误ENOTCONN.按照从处理程序抛出的异常的影响的文件,一个处理程序中抛出的异常被允许通过的投掷线程调用传播起来run(),run_one(),poll(),或poll_one().
要解决此问题,请考虑:
调用非抛出版本basic_stream_socket::remote_endpoint(),并适当地处理错误:
boost::system::error_code ec;
boost::asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(ec);
if (ec)
{
// An error occurred. Stop the asynchronous call chain for
// this connection.
}
Run Code Online (Sandbox Code Playgroud)run()从try/ catch块中调用.文档中提到了以下代码:
boost::asio::io_service io_service;
...
for (;;)
{
try
{
io_service.run();
break; // run() exited normally
}
catch (my_exception& e)
{
// Deal with exception as appropriate.
}
}
Run Code Online (Sandbox Code Playgroud)