Java 如何检测远程端套接字关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/151590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:57:54  来源:igfitidea点击:

How to detect a remote side socket close?

javanetworkingsocketstcp

提问by Kevin Wong

How do you detect if Socket#close()has been called on a socket on the remote side?

你如何检测是否Socket#close()在远程端的套接字上被调用?

采纳答案by WMR

The isConnectedmethod won't help, it will return trueeven if the remote side has closed the socket. Try this:

isConnected方法无济于事,true即使远程端关闭了套接字,它也会返回。尝试这个:

public class MyServer {
    public static final int PORT = 12345;
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(PORT);
        Socket s = ss.accept();
        Thread.sleep(5000);
        ss.close();
        s.close();
    }
}

public class MyClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        Socket s = SocketFactory.getDefault().createSocket("localhost", MyServer.PORT);
        System.out.println(" connected: " + s.isConnected());
        Thread.sleep(10000);
        System.out.println(" connected: " + s.isConnected());
    }
}

Start the server, start the client. You'll see that it prints "connected: true" twice, even though the socket is closed the second time.

启动服务器,启动客户端。您会看到它打印了两次“connected: true”,即使套接字第二次关闭。

The only way to really find out is by reading (you'll get -1 as return value) or writing (an IOException(broken pipe) will be thrown) on the associated Input/OutputStreams.

真正找出答案的唯一方法是IOException在关联的输入/输出流上读取(您将获得 -1 作为返回值)或写入(将抛出(损坏的管道))。

回答by Sangamesh

You can also check for socket output stream error while writing to client socket.

您还可以在写入客户端套接字时检查套接字输出流错误。

out.println(output);
if(out.checkError())
{
    throw new Exception("Error transmitting data.");
}

回答by Thorsten Niehues

Since the answers deviate I decided to test this and post the result - including the test example.

由于答案有所不同,我决定对此进行测试并发布结果 - 包括测试示例。

The server here just writes data to a client and does not expect any input.

这里的服务器只是将数据写入客户端,不需要任何输入。

The server:

服务器:

ServerSocket serverSocket = new ServerSocket(4444);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
  out.println("output");
  if (out.checkError()) System.out.println("ERROR writing data to socket !!!");
  System.out.println(clientSocket.isConnected());
  System.out.println(clientSocket.getInputStream().read());
        // thread sleep ...
  // break condition , close sockets and the like ...
}
  • clientSocket.isConnected() returns always true once the client connects (and even after the disconnect) weird !!
  • getInputStream().read()
    • makes the thread wait for input as long as the client is connected and therefore makes your program not do anything - except if you get some input
    • returns -1 if the client disconnected
  • out.checkError() is true as soon as the client is disconnected so I recommend this
  • 一旦客户端连接(甚至在断开连接之后),clientSocket.isConnected() 总是返回 true 很奇怪!
  • getInputStream().read()
    • 只要客户端连接,线程就会等待输入,因此使您的程序不执行任何操作 - 除非您获得一些输入
    • 如果客户端断开连接,则返回 -1
  • 一旦客户端断开连接,out.checkError() 就为真,所以我推荐这个

回答by DumbestGuyOnSaturn

The method Socket.Available will immediately throw a SocketException if the remote system has disconnected/closed the connection.

如果远程系统断开/关闭连接,方法 Socket.Available 将立即抛出 SocketException。