简体   繁体   English

Android-通过套接字编程连接的Java。 设备与网络断开连接后,dataoutputstream写操作在Java端不会失败

[英]Android - java connected via socket programming. dataoutputstream write does not fail on the java side after the device is disconnected from network

I have a android device sending data to Java server via TCP connection. 我有一个Android设备通过TCP连接将数据发送到Java服务器。 I verify whether the device is connected to server by writing in the device output stream every 10 seconds. 我每10秒写入一次设备输出流,以验证设备是否已连接到服务器。 But when the android device is disconnected from Wifi, the server could still write for sometime - after that it would fail. 但是,当android设备与Wifi断开连接时,服务器仍然可以写入一段时间-之后它将失败。 But if i terminate the application, the server write to the client fails right away. 但是,如果我终止该应用程序,则服务器立即将写入客户端的操作失败。 Any suggestions on where am i going wrong ? 关于我哪里出问题的任何建议?

The first write to a broken connection almost certainly won't fail, due to TCP buffering. 由于TCP缓冲,第一次写入断开的连接几乎肯定不会失败。 A subsequent write will fail. 后续写入将失败。 It could take quite a few, or a lot of data. 这可能需要相当多的数据。 If you terminate the receiver it sends something to the sender that it can detect. 如果终止接收方,它将向发送方发送可以检测到的内容。 If the connection just breaks, TCP has to time out retries. 如果连接刚刚断开,则TCP必须重试超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM