Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket not disconnecting when connectivity changes

My chat application connects to a server and information is sent/received by the user. When the connection changes, such as 3g->wifi, wifi->3g, losing a data connection, etc, the socket sometimes stays connected for ages before disconnecting. During this time, it's impossible to tell if the connection is still active, it seems as if messages are being sent just fine. Other times, when sending a message, it will throw an IO error and disconnect.

Apart from implementing code to detect connection changes and reconnecting appropriately, is it possible to have the socket immediately throw an IO exception when connectivity changes?

Edit: I'm connecting using the following code:

Socket sock = new Socket();
sock.connect(new InetSocketAddress(getAddress(), getPort())), getTimeout());
//get bufferedReader and read until BufferedReader#readLine() returns null

I'm not using setSoTimeout as data may not be transferred for long periods of time depending on the remote server's configuration.

like image 210
Al. Avatar asked Jan 05 '10 19:01

Al.


1 Answers

Are you talking about a java.net.Socket connection? Then try setSoTimeout(). Otherwise specify how you're connecting.

like image 173
Mirko N. Avatar answered Nov 14 '22 23:11

Mirko N.