It takes quite a moment for socket.read_some to return after socket.close has been called, in linux
I'm using Boost.Asio as a simple socket library.
When I open a socket, I create a thread which keeps reading on that socket, and returns when the socket has been closed, or some other errors occured.
while((r开发者_开发技巧ead = socket->read_some(buf, ec)) != 0) {
// deal with bytes read
}
This code works well on Windows and Mac. However with linux, when the socket is closed from the main thread, it takes quite long time for socket::read_some
to return - I found it's more than 2 minutes.
Is there anything I can do to improve this?
If you desire cancel-ability, use asynchronous sockets. Don't use synchronous methods such as read_some
. This has been discussed ad infinitum on the asio-users mailing list. There's also a ticket on the boost bug tracker discussing it.
Also see my answer to a similar question.
Finally I found the reason: in Linux if you close a socket with socket::close, the socket is not closed. You must close a socket gracefully to close it successfully.
socket->shutdown(shutdown_both); // add this
socket->close();
精彩评论