Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if socket is closed in Boost.Asio?

What is the easiest way to check if a socket was closed on the remote side of the connection? socket::is_open() returns true even if it is closed on the remote side (I'm using boost::asio::ip::tcp::socket).

I could try to read from the stream and see if it succeeds, but I'd have to change the logic of my program to make it work this way (I do not want data to be extracted from the stream at the point of the check).

like image 568
Hali Avatar asked May 04 '09 03:05

Hali


1 Answers

Just check for boost::asio::error::eof error in your async_receive handler. It means the connection has been closed. That's the only proper way to do this.

like image 86
dongle26 Avatar answered Oct 23 '22 14:10

dongle26