如何在Boost.Asio中检查套接字是否已关闭?

时间:2022-09-09 12:59:49

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).

检查套接字是否在连接的远程端关闭的最简单方法是什么? socket :: is_open()返回true,即使它在远程端关闭(我使用的是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).

我可以尝试从流中读取并查看它是否成功,但我必须更改程序的逻辑以使其以这种方式工作(我不希望在检查时从流中提取数据)。

5 个解决方案

#1


If the connection has been cleanly closed by the peer you should get an EOF while reading. Otherwise I generally ping in order to figure out if the connection is really alive.

如果同伴干净地关闭了连接,您应该在阅读时获得EOF。否则我通常ping,以确定连接是否真的活着。

#2


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.

只需在async_receive处理程序中检查boost :: asio :: error :: eof错误。这意味着连接已关闭。这是唯一正确的方法。

#3


Is there a boost peek function available? Most socket implementations have a way to read data without removing it from the queue, so you can read it again later. This would seem to satisfy your requirements.

是否有可用的增强功能?大多数套接字实现都有一种方法来读取数据而无需将其从队列中删除,因此您可以稍后再次读取它。这似乎满足您的要求。

After quickly glancing through the asio docs, I wasn't able to find exactly what I was expecting, but that doesn't mean its not there.

在快速浏览asio文档之后,我无法找到我期待的确切内容,但这并不意味着它不在那里。

I'd suggest this for starters.

我建议初学者。

#4


I think that in general once you open a socket, you should start reading it inmediately and never stop doing so. This way you can make your server or client to support both synchronous and asynchronous protocols. The moment the client closes the connection, the moment the read will tell you this.

我认为一般来说,一旦你打开一个套接字,你应该立即开始阅读它,永远不要停止这样做。这样,您可以使服务器或客户端同时支持同步和异步协议。客户端关闭连接的那一刻,读取将告诉您的那一刻。

#5


Using error_code is able to check the condition whether the client is connected or not. If the connection is success, the error_code error.value() will return 0, else return other value. You can also check the message() from the error_code.

使用error_code可以检查客户端是否连接的条件。如果连接成功,则error_code error.value()将返回0,否则返回其他值。您还可以从error_code检查消息()。

#1


If the connection has been cleanly closed by the peer you should get an EOF while reading. Otherwise I generally ping in order to figure out if the connection is really alive.

如果同伴干净地关闭了连接,您应该在阅读时获得EOF。否则我通常ping,以确定连接是否真的活着。

#2


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.

只需在async_receive处理程序中检查boost :: asio :: error :: eof错误。这意味着连接已关闭。这是唯一正确的方法。

#3


Is there a boost peek function available? Most socket implementations have a way to read data without removing it from the queue, so you can read it again later. This would seem to satisfy your requirements.

是否有可用的增强功能?大多数套接字实现都有一种方法来读取数据而无需将其从队列中删除,因此您可以稍后再次读取它。这似乎满足您的要求。

After quickly glancing through the asio docs, I wasn't able to find exactly what I was expecting, but that doesn't mean its not there.

在快速浏览asio文档之后,我无法找到我期待的确切内容,但这并不意味着它不在那里。

I'd suggest this for starters.

我建议初学者。

#4


I think that in general once you open a socket, you should start reading it inmediately and never stop doing so. This way you can make your server or client to support both synchronous and asynchronous protocols. The moment the client closes the connection, the moment the read will tell you this.

我认为一般来说,一旦你打开一个套接字,你应该立即开始阅读它,永远不要停止这样做。这样,您可以使服务器或客户端同时支持同步和异步协议。客户端关闭连接的那一刻,读取将告诉您的那一刻。

#5


Using error_code is able to check the condition whether the client is connected or not. If the connection is success, the error_code error.value() will return 0, else return other value. You can also check the message() from the error_code.

使用error_code可以检查客户端是否连接的条件。如果连接成功,则error_code error.value()将返回0,否则返回其他值。您还可以从error_code检查消息()。