Boost.Asio套接字析构函数关闭连接? [英] Boost.Asio socket destructor closes connection?
问题描述
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?
推荐答案
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
析构函数,destroy()
将在I/O对象的I/O服务上调用,并传入implementation_type
的实例,I/O对象的服务将在该实例上运行.对于套接字,destroy()
将在满足
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)
一样.
a.close(b, ec)
:
如果
a.is_open()
为true,则使所有未完成的异步操作尽快完成.取消操作的处理程序应传递错误代码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套接字析构函数关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!