cihm
cihm

Reputation: 71

android server how to know client is disconnect or close

i have two android device, A is server , B is client , and connect with socket , how do server/client know each other is disconnect or close ,

i have try isconnect(), but not useful.

Upvotes: 2

Views: 1824

Answers (1)

user207421
user207421

Reputation: 310913

If the peer has disconnected normally:

  • read() will return -1
  • readLine() will return null
  • readObject(), readUTF(), readXXX() for any XXX will throw `EOFException

If the peer has disconnected abnormally, the connection has gone bad, etc, any read or write method will throw an IOException, except that a read without a read timeout may block forever: this is why you should always use a read time (via setSoTimeout()).

isConnected() only tells you whether you ever connected or accepted the socket. isClosed() only tells you whether you ever closed it. These APIs tell you about the Socket, not the connection.

Upvotes: 5

Related Questions