1
votes

According to boost documentation, when socket::close() is invoked, asynchronous send, receive or connect operations will be cancelled immediately, and will complete with the boost::asio::error::operation_aborted error.

socket::cancel causes all outstanding asynchronous connect, send and receive operations to finish immediately, and the handlers for cancelled operations will be passed the boost::asio::error::operation_aborted error.

Is there some difference between them?

When I want to finish one socket connection, which one should I call such that their callback handler will be invoked with boost::asio::error::operation_aborted error?

1

1 Answers

6
votes

Is there some difference between them?

Yes. The close() method closes the socket, while the cancel() method cancels outstanding asynchonous operations. Using cancel() does not close the socket.

When I want to finish one socket connection, which one I should to call?

You should use cancel() to stop any outstanding asynchronous operations. The socket will be closed by its destructor.