ObjectOutputStream IOException / timeout on Android

为君一笑 提交于 2020-01-17 05:27:33

问题


I am developing a real-time Android application and have an issue with detecting errors on an ObjectOutputStream which is connected to a TCP network Socket. I am calling writeObject followed by a flush on the stream as normal, however when the receiving node unexpectedly dies I need to know about it right away (within seconds). Unfortunately when the node goes down the server's call to writeObject and flush do not throw IOExceptions and instead succeed.

I am assuming there is internal buffering that takes place with these calls and that is the reason for its success? Is there any way to know when a node is down via writeObject or other means after a Socket has been successfully created?

It is worth noting that the TCP Sockets are left open for performance reasons, so setting a timeout on connect for every writeObject is not possible.


回答1:


when the receiving node unexpectedly dies I need to know about it right away (within seconds)

You can't. TCP doesn't tell you. It doesn't know. It is busy doing buffering and retrying. It will wventually time out, and if you do a write after that you will get the exception. There is no control whatsoever over how long that takes.



来源:https://stackoverflow.com/questions/5800773/objectoutputstream-ioexception-timeout-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!