Reputation: 71
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
Reputation: 310913
If the peer has disconnected normally:
read()
will return -1readLine()
will return nullreadObject(),
readUTF()
, readXXX()
for any XXX will throw `EOFExceptionIf 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