Java Socket 类在连接状态方面撒谎
我们有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仔细阅读 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.
我对此不是 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()
orwrite()
fail is a pretty reliable way to tell that the stream has been torn down.Elliot Rusty Harold 的《Java 网络编程》提供了以下精华:
“...要判断套接字当前是否打开,您需要检查 isConnected() 返回 true 并且 isClosed() 返回 false。例如:
希望这会有所帮助。
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:
Hope this helps.