In Android, how to disable a socket?

So, I have a socket that is connected to an external web address, and when it receives a specific message, which it should disconnect. I tried calling socket.close() , but socket.isConnected() is still correct. No luck finding an answer

+8
android sockets disconnect
source share
4 answers

isConnected() only tells you if you have successfully connected to the socket. isClosed() reports that you called close() .

Refuse the answer of these guys

+4
source share

try

 socket.isClosed() 

Returns boolian whether this socket is closed. handle sockets

0
source share

"When you use Socket (), which you seem to be missing, Socket.isConnected () tells you whether Socket.connect () or not. Likewise for isClosed () and close ().

Confusion regarding these methods leads to confusion of the state of the socket, which is under the control of the application, with the state of the general connection, which is under the control of the protocol. isConnected () and isClosed () tell me what you did with the socket. There is no API other than reading and writing to determine the state of a connection. "

Oracle documantion <here he explained well that as soon as you call .close() , the connection is closed and you can check it for isClosed() .

.close () :

Closes this socket.

Any thread that is currently blocked in an I / O operation on this socket will throw a SocketException.

Once the socket has been closed, it is inaccessible for further use of the network (i.e. cannot be reconnected or rebound). A new connector must be created.

0
source share

http://developer.android.com/reference/java/net/Socket.html#close ()

Make a checksum for the returned data, make sure you get the correct value. If so, and you called closely and tried to catch everything, it should be fine.

You can look in the debugger and make sure that nothing is leaking.

0
source share

All Articles