本文介绍了Java:检测客户端从服务器端断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Socket和ServerSocket编写一个Java客户端/服务器程序。多个客户端立即连接到服务器。检查客户端连接是否已从服务器端断开连接的最佳方法是什么?现在,我只是得到一个SocketException尝试写入断开的客户端。
I am writing a Java client/server program using Socket and ServerSocket. Multiple clients connect to the server at once. What is the best way to check if a client connection has disconnected from the server side? Right now, I'm just getting a SocketException when attempting to write to a disconnected client.
推荐答案
检测断开连接的最佳方法是让另一端发送心跳,数据。
The best way to detect a disconnection is to have the other end send a heartbeat and have the reader timeout when there is no data.
这篇关于Java:检测客户端从服务器端断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!