Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a java socket channel which should be closed does not generate an exception

We have a java server that keeps a socket channel open with an Android client in order to provide push capabilities to our client application.

However, after putting the Android in airplane mode, which I expected would sever the connection, the server can still write to the SocketChannel object associated with that Android client and no error is thrown. Calling SocketChannel.isConnected() before writing to it returns true.

What are we missing? Is the handling of sockets different with mobile devices?

Thanks in advance for your help.

like image 706
Dan Serfaty Avatar asked Feb 02 '26 09:02

Dan Serfaty


1 Answers

SocketChannel.socket().isConnected() and SocketChannel.isConnected() return false before the socket is connected. Once the socket is connected they will return true, they will not revert to false for any reason.

SocketChannel.isOpen() returns true until you call SocketChannel.socket().close() or SocketChannel.close(). Once you have called close() on the socket, SocketChannel.isOpen() will return false, no other event will cause it to return false.

SocketChannel.socket().isClosed() returns false until you call SocketChannel.socket().close() or SocketChannel.close(). Once you have called close() on a socket - SocketChannel.socket().isClosed() will return true, no other event will cause it to return true.

like image 74
martinpelant Avatar answered Feb 04 '26 23:02

martinpelant