Java Socket 类在连接状态方面撒谎

发布于 2024-10-04 13:49:38 字数 732 浏览 0 评论 0原文

我们有一个 Java 客户端,它保持与远程服务器打开的持久套接字连接。客户端每 15 秒轮询一次数据库表,如果有新项目,它将序列化并将其写入套接字。

在写入输出流之前,我想检查套接字连接是否仍然良好。对于特定的应用程序逻辑,执行此主动检查比捕获异常并被动地重新连接更简单。

我使用以下代码找出连接断开时哪种方法可以让我知道:

LOG.debug("Socket status: bound=" + _socket.isBound() + ", closed=" + _socket.isClosed() + ", connected=" + _socket.isConnected() + ", outputShutdown=" + _socket.isOutputShutdown() + ", inputShutdown=" + _socket.isOutputShutdown());

我短暂禁用了网络适配器,并且在下一次轮询期间,如预期的那样,写入套接字时出现异常。

但是,调试语句打印了以下内容:

"Socket status: bound=true, closed=false, connected=true, outputShutdown=false, inputShutdown=false"

我预计 close 为 true 或 connect 为 false。我得到的实际值似乎是一个谎言。

有没有办法可靠地检查套接字的连接状态?

We have a Java client that keeps a persistent socket connection open to a remote server. The client polls a DB table every 15 seconds and if there a new item, it serializes and writes it to the socket.

Before writing to the output stream, I would like to check whether socket connection is still good. For the specific application logic doing this proactive check is simpler than catching an exception and reconnecting reactively.

I used following code figure out which method can let me know when the connection is broken:

LOG.debug("Socket status: bound=" + _socket.isBound() + ", closed=" + _socket.isClosed() + ", connected=" + _socket.isConnected() + ", outputShutdown=" + _socket.isOutputShutdown() + ", inputShutdown=" + _socket.isOutputShutdown());

I briefly disable my network adapter and during the next polling, as expected, there was an exception while writing to the socket.

However, the debug statement printed the following:

"Socket status: bound=true, closed=false, connected=true, outputShutdown=false, inputShutdown=false"

I expected either closed to be true or connected to be false. What actual values I get seem to be a lie.

Is there a way to reliably check the connection status of a socket?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-10-11 13:49:38

仔细阅读 Socket 类 Javadoc

isConnected 为 true套接字是否能够连接。方法名称用词不当,如果是 hasConnected 会更准确。

一旦套接字成功连接,它就变为 true 并保持 true。 isBound。您必须尝试套接字操作并检查是否失败。

Read the Socket class Javadoc carefully.

isConnected is true if the socket was able to connect. The method name is a misnomer, it would more accurate if it was hasConnected.

Once the socket successfully connects it becomes true and stays true. Same thing for isBound. You have to try a socket operation and check for failure.

煮茶煮酒煮时光 2024-10-11 13:49:38

我对此不是 100% 确定,但我非常确定底层 BSD Sockets API 实际上没有一种机制来确定 TCP 流是否仍然打开; read()write() 失败是判断流已被拆除的非常可靠的方法。

I'm not 100% sure about this, but I'm pretty certain that the underlying BSD Sockets API doesn't actually have a mechanism to determine whether or not a TCP stream is still open; having a read() or write() fail is a pretty reliable way to tell that the stream has been torn down.

行至春深 2024-10-11 13:49:38

Elliot Rusty Harold 的《Java 网络编程》提供了以下精华:

“...要判断套接字当前是否打开,您需要检查 isConnected() 返回 true 并且 isClosed() 返回 false。例如:

boolean connected = socket.isConnected() && ! socket.isClosed();"

希望这会有所帮助。

Java Network Programming by Elliot Rusty Harold offers the following gem:

"...To tell if a socket is currently open, you need to check that isConnected() returns true and isClosed() returns false. For example:

boolean connected = socket.isConnected() && ! socket.isClosed();"

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文