你如何处理 Java 中的套接字断开连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/485855/
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 15:15:46  来源:igfitidea点击:

How do you handle Socket Disconnecting in Java?

javasockets

提问by Alex

Hey all. I have a server written in java using the ServerSocket and Socket classes.

大家好。我有一个使用 ServerSocket 和 Socket 类用 Java 编写的服务器。

I want to be able to detect and handle disconnects, and then reconnect a new client if necessary.

我希望能够检测和处理断开连接,然后在必要时重新连接新客户端。

What is the proper procedure to detect client disconnections, close the socket, and then accept new clients?

检测客户端断开连接、关闭套接字然后接受新客户端的正确程序是什么?

采纳答案by Rob

Presumably, you're reading from the socket, perhaps using a wrapper over the input stream, such as a BufferedReader. In this case, you can detect the end-of-stream when the corresponding read operation returns -1 (for raw read() calls), or null (for readLine() calls).

据推测,您正在从套接字读取,可能在输入流上使用包装器,例如 BufferedReader。在这种情况下,您可以在相应的读取操作返回 -1(对于原始 read() 调用)或 null(对于 readLine() 调用)时检测流结束。

Certain operations will cause a SocketException when performed on a closed socket, which you will also need to deal with appropriately.

某些操作在关闭的套接字上执行时会导致 SocketException,您也需要适当处理。

回答by Peter Lawrey

The only safe way to detect the other end has gone is to send heartbeats periodically and have the other end to timeout based on a lack of a heartbeat.

检测另一端已消失的唯一安全方法是定期发送心跳,并让另一端根据缺少心跳而超时。

回答by steve_stackex

Is it just me, or has nobody noticed that the JavaDoc states a method under ServerSocket api, which allows us to obtain a boolean based on the closed state of the serversocket?

难道只有我,还是没有人注意到 JavaDoc 在ServerSocket api下声明了一个方法,它允许我们根据 serversocket 的关闭状态获取布尔值?

you can just loop every few seconds to check the state of it:

您可以每隔几秒钟循环一次以检查它的状态:

if(!serverSocket.isClosed()){
  // whatever you want to do if the serverSocket is connected
}else{
  // treat a disconnected serverSocket
}

EDIT: Just reading your question again, it seems that you require the server to just continually search for connections and if the client disconnects, it should be able to re-detect when the client attempts to re-connect. should'nt that just be your solution in the first place?

编辑:再次阅读您的问题,似乎您需要服务器不断搜索连接,如果客户端断开连接,它应该能够重新检测客户端何时尝试重新连接。这不应该只是你的解决方案吗?

Have a server that is listening, once it picks up a client connection, it should pass it to a worker thread object and launch it to operate asynchronously. Then the server can just loop back to listening for new connections. If the client disconnects, the launched thread should die and when it reconnects, a new thread is launched again to handle the new connection.

有一个正在监听的服务器,一旦它接收到一个客户端连接,它应该将它传递给一个工作线程对象并启动它以异步操作。然后服务器可以循环回侦听新连接。如果客户端断开连接,启动的线程应该死,当它重新连接时,会再次启动一个新线程来处理新连接。

Jenkovprovides a great example of this implementation.

Jenkov提供了这个实现的一个很好的例子。