Chaitanya
Chaitanya

Reputation: 2049

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?

Thanks,

Upvotes: 2

Views: 1287

Answers (2)

Chris Dennett
Chris Dennett

Reputation: 22741

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.

Upvotes: 1

Richard H
Richard H

Reputation: 39135

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.

Upvotes: 0

Related Questions