java/android TCP sockets - detecting whether the server is offline
I am developing an Android application that connects to a TCP server to upload some data. I needed to detect whether the server is offline to know if my uploading is successful.
After some amount of looking around, I have decided to use NullPointerException in write() of OutputStream as the indicator.
Although this works for my case (Server implemented by me, have to only write() from client), I would like to know about a better/standard way to detect if the other endpoint of TCP connection is down or has gone down.
(I have already looked at a few related questions here on stackoverflow.)
Any comments/inputs?
开发者_JS百科Thanks,
The SocketIOException
/ IOException
saying that the remote host isn't responding should be passed through automatically, providing you don't catch the exception. It's all built-in.
The only way to know is to try connecting to the server. Specify some kind of timeout; if the server doesn't respond in that time then handle appropriately.
精彩评论