提升asio basic_socket问题
boost asio basic_socket issue
我是一名游戏开发人员。我在开发聊天功能时遇到了这个问题。我的游戏在Iphone上运行时崩溃了,这是由asio lib中的basic_socket::close引起的。这是源代码:
/// Close the socket.
/**
* This function is used to close the socket. Any asynchronous send, receive
* or connect operations will be cancelled immediately, and will complete
* with the boost::asio::error::operation_aborted error.
*
* @throws boost::system::system_error Thrown on failure. Note that, even if
* the function indicates an error, the underlying descriptor is closed.
*
* @note For portable behaviour with respect to graceful closure of a
* connected socket, call shutdown() before closing the socket.
*/
void close()
{
boost::system::error_code ec;
this->get_service().close(this->get_implementation(), ec);
boost::asio::detail::throw_error(ec, "close");
}
所以我的问题是,为什么它总是抛出一个异常?(顺便说一句,如果你在boost上不使用异常功能,throw_error方法最终会调用std::terminate(),这会使程序崩溃。)
---------------------------更新-------------------------
我的游戏可能会关闭http请求并重新启动它。当它关闭请求时,它会转到这里关闭套接字。我只是不知道为什么它会在接近时抛出异常,我认为这是没有必要的,不是吗?
我已经用try&接住在boost中的无使用异常情况下,我调用std::set_terminate()来避免崩溃。所以我不要求解决方案,而是询问原因:)
basic_socket::close()
是操作系统特定的::close()
或::closesocket()
调用的精简包装器。::close()
的iOS文档指出,如果出现以下情况,它将失败:
[EBADF]
-不是有效的活动文件描述符[EINTR]
-执行被一个信号中断[EIO]
—以前未提交的写入遇到输入/输出错误
检查异常或error_code
以确定故障类型。
正如Boost.Asio basic_socket::close()
文档中所建议的那样,应该考虑在关闭套接字之前调用shutdown()
,以获得优雅关闭时的可移植行为。此外,考虑对函数使用非抛出重载,例如这个basic_socket::close(ec)
重载:
boost::asio::ip::tcp::socket socket(io_service);
boost::system::error_code error;
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if (error)
{
// An error occurred.
}
socket.close(error);
if (error)
{
// An error occurred.
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- IPv6 Socket程序问题