In Android how do I disconnect a socket?
Asked Answered
M

6

9

So I've got a socket that is connected to an outside web address and when it gets a certain message it's supposed to disconnect. I tried calling socket.close() but socket.isConnected() is still true. No luck searching for an answer

Malaco answered 23/8, 2013 at 15:31 Comment(0)
A
5

isConnected() only tells you if you made a successful connection to a socket. isClosed() tells you if you called close().

Check out this guys response https://mcmap.net/q/446417/-how-can-a-socket-be-both-connected-and-closed

Aksoyn answered 23/8, 2013 at 16:34 Comment(2)
Ok so the next question is does calling close kill the connection? And if not what will?Malaco
Calling close() closes your side of the socket. The server isn't notified that you closed the socket though.Aksoyn
P
0

" When you use Socket(), which you seem to have overlooked, Socket.isConnected() tells you whether Socket.connect() has been called or not. Similarly for isClosed() and close().

Confusion over these methods results from confusing the state of the socket, which is under the control of the application, with the state of the overall connection, which is under the control of the protocol. isConnected() and isClosed() tell what you have done to the socket. There are no APIs other than read and write for determining the state of the connection."

Oracle documantion < in here it's well explained that once you call .close() the connection is closed and you can check by isClosed().

.close() :

Closes this socket.

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

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

Perfectionism answered 23/8, 2013 at 16:41 Comment(0)
W
0

isConnected() and isClosed() tell you the current state of your socket (in your side). isConnected() tells you whether you have connected this socket and isClosed() tells you whether you have closed this socket. Until you have, it returns false.

Waac answered 3/1, 2019 at 12:37 Comment(0)
R
0

Yes, right. It surely seems socket.close() and socket.isConnected() are independent from each other. I wish method naming was not as confusing.

From official Android SDK:

public boolean isConnected()
Returns the connection state of the socket.

Note: Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket

P.S. more intuitively it should be called wasConnected() 😅

Retentive answered 16/7, 2021 at 8:28 Comment(0)
S
-1

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

Do a checksum on the data returned, make sure you get the correct value. If so and you called close and try-catch everything, it should be fine.

You could watch in a debugger and make sure nothing is leaking.

Southeastwardly answered 23/8, 2013 at 16:50 Comment(0)
G
-2

try

socket.isClosed() 

Returns a boolian, whether this socket is closed. Refer Sockets

Gypsophila answered 23/8, 2013 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.