我在这里陷入了一个难题……</p>
我的客户端 SSLSocket 连接到服务器(不是 Java)并按预期成功读写二进制数据。然后,在某个时间点,服务器断开客户端的连接。但是,客户端仍然可以在startHandshake()
不引发异常的情况下调用,而且我看不到任何迹象表明连接已断开,除了getLocalSocketAddress()
返回全 0 的 IP。
代码如下所示:
SSLSocket socket = (SSLSocket)socketFactory.createSocket(host, port);
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
/* Socket happily sends/receives data */
socket.getOutputStream().write(myData);
socket.getOutputStream().flush();
socket.getInputStream().read(myBuffer);
// do something with myBuffer
Thread.sleep(20 * 1000); // During this wait period, server disconnects the client
socket.getLocalSocketAddress(); // returns host:port as expected
socket.startHandshake(); // doing another read/write will also produce the same result
socket.getLocalSocketAddress(); // returns an IP of all zeroes: /0:0:0:0:0:0:1129:6a1f%0:51447
socket.isClosed(); // somehow, returns false!
socket.isConnected(); // somehow, returns true!
socket.getOutputStream.write(myOtherData); // throws a SocketException: Broken pipe.
getLocalSocketAddress()
基本上,我想我的问题是,除了检查IP 是否全为 0之外,是否有其他方法可以确定服务器是否断开了连接?