简体   繁体   English

Java Socket类说谎关于连接状态

[英]Java Socket class is lying regarding connection status

We have a Java client that keeps a persistent socket connection open to a remote server. 我们有一个Java客户端,该客户端使持久的套接字连接保持与远程服务器的连接。 The client polls a DB table every 15 seconds and if there a new item, it serializes and writes it to the socket. 客户端每15秒轮询一次数据库表,如果有新项,它将进行序列化并将其写入套接字。

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? 有没有一种方法可以可靠地检查套接字的连接状态?

Read the Socket class Javadoc carefully. 仔细阅读Socket类Javadoc

isConnected is true if the socket was able to connect. 如果套接字能够连接,则isConnected为true。 The method name is a misnomer, it would more accurate if it was hasConnected . 方法名称是错误的名称,如果它具有hasConnected ,它将更准确。

Once the socket successfully connects it becomes true and stays true. 一旦套接字成功连接,它将变为true并保持为true。 Same thing for isBound . 对于isBound同样如此。 You have to try a socket operation and check for failure. 您必须尝试执行套接字操作并检查是否失败。

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; 我对此不是100%的确定,但是我可以肯定,底层的BSD套接字API实际上没有确定TCP流是否仍处于打开状态的机制。 having a read() or write() fail is a pretty reliable way to tell that the stream has been torn down. read()write()失败是一种非常可靠的方式来告知流已被拆除。

Java Network Programming by Elliot Rusty Harold offers the following gem: Elliot Rusty Harold撰写的Java Network Programming提供了以下宝石:

"...To tell if a socket is currently open, you need to check that isConnected() returns true and isClosed() returns false. For example: “ ...要判断套接字当前是否打开,您需要检查isConnected()返回true,isClosed()返回false。例如:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM