Boost.Asio socket destructor closes connection?(Boost.Asio 套接字析构函数关闭连接?)
问题描述
boost::asio::ip::tcp::socket
的析构函数究竟做了什么?即使在搜索 Boost 文档和源代码之后,我也无法判断是否需要使用
What exactly does the destructor of boost::asio::ip::tcp::socket
do? I can't tell, even after scouring Boost docs and source code, if I need to use
socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
socket->close();
打电话之前
delete socket;
我需要手动关闭套接字,还是由析构函数处理?
Do I need to close the socket manually, or does the destructor handle this?
推荐答案
当一个socket被销毁时,它会被as-if关闭.org/doc/libs/1_61_0/doc/html/boost_asio/reference/basic_socket/close/overload2.html" rel="noreferrer">socket.close(ec)
在破坏套接字.
When a socket is destroyed, it will be closed as-if by socket.close(ec)
during the destruction of the socket.
I/O 对象,例如 socket
,派生自 basic_io_object
.在 basic_io_object
destructor, destroy()
将在 I/O 对象的 I/O 服务上调用,传入 I/O 对象的 implementation_type
实例O 对象的服务将运行.在套接字的情况下,destroy()
将在满足 SocketService 类型要求,关闭底层socket.在下面的文档中,a
是套接字服务类的实例,b
是套接字服务类的 implementation_type
的实例:
I/O objects, such as socket
, derive from basic_io_object
. Within the basic_io_object
destructor, destroy()
will be invoked on the I/O object's I/O service, passing in an instance of the implementation_type
on which the I/O object's service will operate. In the case of socket, destroy()
will be invoked on a type that fulfills the SocketService type requirement, closing the underlying socket. In the documentation below, a
is an instance of a socket service class, and b
is an instance of the implementation_type
for the socket service class:
a.destroy(b)
:
[...] 隐式取消异步操作,就像调用 a.close(b, ec)
.
[...] Implicitly cancels asynchronous operations, as if by calling
a.close(b, ec)
.
a.close(b, ec)
:
如果 a.is_open()
为真,则导致所有未完成的异步操作尽快完成.取消操作的处理程序应传递错误代码 error::operation_aborted
.
If
a.is_open()
is true, causes any outstanding asynchronous operations to complete as soon as possible. Handlers for cancelled operations shall be passed the error codeerror::operation_aborted
.
帖子:!a.is_open(b)
.
这篇关于Boost.Asio 套接字析构函数关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Boost.Asio 套接字析构函数关闭连接?
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01