Question

I'm working on a NIO client program on Android, Basically it functions well, but when the network is not avaliable(WIFI is disabled, etc), I have no idea how to distinguish the socket is 'really' connected, SocketChannel.finishConnect() always returns true even there is no avaliable network.If I try to write to a unconnected SocketChannel, an Broken pipe exception will happen.

So I wonder how to make sure the SocketChannel is really connected?

Was it helpful?

Solution

If I try to write to a unconnected SocketChannel, an Broken pipe exception will happen.

Correct.

So I wonder how to make sure the SocketChannel is really connected?

That's how. There is no API that can tell you. TCP doesn't work like that. The only way to detect a broken TCP connection is to try to use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top