问题描述
我们有我们的移动设备和我们的服务器既用Java编写的一个简单的客户端服务器架构。一个非常简单的ServerSocket和Socket实现。但一个问题是,当客户端突然终止(没有正确关闭套接字)服务器不知道它是断开的。此外,该服务器可以继续写这个套接字没有得到任何的异常。为什么?
We have a simple client server architecture between our mobile device and our server both written in Java. An extremely simple ServerSocket and Socket implementation. However one problem is that when the client terminates abruptly (without closing the socket properly) the server does not know that it is disconnected. Furthermore, the server can continue to write to this socket without getting any exceptions. Why?
据文件,如果你尝试写入一个套接字无法到达另一端的Java插座应抛出异常!
According to documentation Java sockets should throw exceptions if you try to write to a socket that is not reachable on the other end!
推荐答案
连接最终将超时重传由超时(RTO)。但是,RTO使用基于网络延迟(RTT)一个复杂的算法计算出的,看到这个RFC,
The connection will eventually be timed out by Retransmit Timeout (RTO). However, the RTO is calculated using a complicated algorithm based on network latency (RTT), see this RFC,
http://www.ietf.org/rfc/rfc2988.txt
所以在移动网络上,这可以是分钟。等待10分钟,看看你能得到一个超时。
So on a mobile network, this can be minutes. Wait 10 minutes to see if you can get a timeout.
要解决这类问题的方法是增加一个心脏搏动在自己的应用协议和拆除连接时,你没有得到ACK心跳。
The solution to this kind of problem is to add a heart-beat in your own application protocol and tear down connection when you don't get ACK for the heartbeat.
这篇关于Java的插座不抛出异常的死插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!