java测试socket是否连接_检查ClientSocket在Java中是否已断开连接

小编典典

创建套接字时,请先设置超时:

private int timeout = 10000;

private int maxTimeout = 25000;

clientSocket.setSoTimeout(timeout);

这样,如果读取超时,您将获得java.net.SocketTimeoutException(必须抓住)。因此,您可以执行以下操作,假设您之前已如上所示设置了SO_TIMEOUT,并假定心跳将始终从远程系统获得响应:

volatile long lastReadTime;

try {

bufferedReader.read();

lastReadTime = System.currentTimeMillis();

} catch (SocketTimeoutException e) {

if (!isConnectionAlive()) {

logger.info("CONNECTION TERMINATED!");

clientSocket.close();

setUpSocket(); //sets up the server to reconnect to the client

} else {

sendHeartBeat(); //Send a heartbeat to the client

}

}

public boolean isConnectionAlive() {

return System.currentTimeMillis() - lastReadTime < maxTimeout;

}

解决此问题的一种常见方法是将超时设置为某个数字(例如10秒),然后跟踪上一次成功从套接字读取的时间。如果您的超时时间已达到2.5倍,请放弃客户端并关闭套接字(以防万一,将FIN数据包发送到另一端)。

如果心跳信号 _不会_从远程系统获得任何响应,而只是在连接断开时最终最终更早地产生IOException的一种方式,那么您可以这样做(假设sendHeartBeat本身不会抛出IOException):

try {

if (bufferedReader.read() == -1) {

logger.info("CONNECTION TERMINATED with EOF!");

resetConnection();

}

} catch (SocketTimeoutException e) {

// This just means our read timed out ... the socket is still good

sendHeartBeat(); //Send a heartbeat to the client

} catch (IOException e) {

logger.info("CONNECTION TERMINATED with Exception " + e.getMessage());

resetConnection();

}

....

private void resetConnection() {

clientSocket.close();

setUpSocket(); //sets up the server to reconnect to the client

}

2020-10-12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值