gpt4 book ai didi

c++ - 使用 boost::asio::ip::tcp::socket::cancel() 和 socket::close()

转载 作者:太空狗 更新时间:2023-10-29 19:46:52 26 4
gpt4 key购买 nike

如果我使用关闭而不是取消,会有一些问题。

close 函数可以关闭套接字,并通过返回 boost::asio::error::operation_aborted 错误来停止任何未完成的异步操作。

为什么我应该使用cancel而不是close

我担心如果一些异步操作正在执行,cancel 无法取消它,是吗?

asio::ip::tcp::resolve::cancel,我多次尝试在调用async_resolve后取消resolve_handler , 但 resolve_handler 返回时总是没有 boost::asio::error::operation_aborted 错误。

我认为 resolve_handler 正在执行?

是吗?

最佳答案

如果您想在不关闭套接字的情况下停止挂起的操作,则取消很有用。

请注意 Boost documentation建议使用 close 以获得更好的可移植性(来自文档页面):

... For portable cancellation, consider using one of the following alternatives:

  • Disable asio's I/O completion port backend by defining BOOST_ASIO_DISABLE_IOCP.
  • Use the close() function to simultaneously cancel the outstanding operations and close the socket.

关于c++ - 使用 boost::asio::ip::tcp::socket::cancel() 和 socket::close(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6434316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com