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.


Asked by: Kevin695 | Posted: 20-01-2022






Answer 1

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

Answered by: Kellan866 | Posted: 21-02-2022



Answer 2

This is an old problem that I've seen a few times before in the database world.

The solution I used there was to manage the connection at the application level. I'd explicitly send a no-op message of some sort (i.e. SELECT 1 WHERE FALSE) over the connection every so often as a ping, and if this failed I would tear down and re-establish the connection, possibly to a failover server if the original wasn't accepting connections.

Answered by: Chelsea451 | Posted: 21-02-2022



Answer 3

As previous answers already pointed out, this is a common problem. Even after sending a custom "ping" it might need some time until the socket realizes that the underlying connection is broken. Plus, regular pings are quite energy-demanding using 3-4G mobile networks, due to their tail states. Don't do that!

What you can do, however, is requesting to get informed when the connectivity changes (last section), and close/reconnect the socket manually in the according broadcast receiver. (EDIT: I see you already found out about this; just keeping it here for completeness)

Answered by: Chloe149 | Posted: 21-02-2022



Similar questions

mobile - Android Check Wifi Connectivity Without Disconnecting 3G

Currently investigating this area will update this post if / when i find anything out. Any extra brain power will be much appreciated on this. similar but not duplicate thread - Android : Check 3G or Wifi network is ON or Available or not on android Device Use case.


emulation - android emulator disconnecting from DDMS view when accessing file explorer

when i go to the DDMS view to access data folder of my app on an emulator, every thing works fine but after a few seconds the list of devices becomes empty which means that the DDMS lost connection with the emulator and even if i restart the adb server it keeps disconnecting and worst it doesnt even show the processes running on that emulator for the short perid of time when it is visible on the devices tab. please...


android - Eclipse debugger is arbitrarily disconnecting

I'm building an Android app with 5 activities and one application (pseudo-singleton) containing a chunk of ndk code. I've recently added a few routines in the ndk, all of which are stable, but since then my app has been acting erratically: my singleton seems to reset, and my activity goes back a level for reasons I have yet to figure out. One of the things that's hindering my understanding is that the debugger is disconnec...


android - How do I keep Wifi from disconnecting when phone is asleep?

I have a service which polls a server at certain intervals. I use an AlarmManager and a BroadcastReceiver to start the service. My problem is that after a certain duration, even though the Wifi is still enabled, but for some reason, my application can't contact the server. I get an "Unreachable network" error. Note that I've already acquired a partial wake lock as well as a wifilock. Here's my code for th...


android - Finishing activity while disconnecting the usb cable

My application can be launched in two ways. 1. Manual launch. (By clicking the launcher icon from the launcher menu) 2. Automatic launch(By connecting the USB cable to the device) The application can be finished in two ways. 1. Manual finish (Pressing the back button) 2. Automatic finish (Disconnecting the USB cable) I have written a broadcast receiver and it will launch the application if USB cable connec...


Android System.err while disconnecting from Bluetooth

I'm trying to disconnect from my Bluetooth device as gracefully as possible, but Android insists on throwing me a system error: 09-02 15:16:16.748: W/System.err(31038): java.io.IOException: Operation Canceled 09-02 15:16:16.748: W/System.err(31038): at android.bluetooth.BluetoothSocket.readNative(Native Method) 09-02 15:16:16.748: W/System.err(31038): at android.bluetooth.BluetoothSocket.read(Bluetoot...


Android: exception when disconnecting from bluetooth device

I'm looking for a way to gracefully disconnect from a Bluetooth device. I used the BluetoothChat sample and the problem is that when you intend to disconnect you simply call socket.close() from the cancel() method, but then the IntputStream.read() will inevitably throw an exception. I need this mainly because it acts as a "lost connection" failsafe as well, so when I ...


mobile - Android Check Wifi Connectivity Without Disconnecting 3G

Currently investigating this area will update this post if / when i find anything out. Any extra brain power will be much appreciated on this. similar but not duplicate thread - Android : Check 3G or Wifi network is ON or Available or not on android Device Use case.


android - Call disconnecting just after dialing while using call intent from service

I'm trying to initiate a call from background service. Its working fine on sony xperia p android ics. But in samsung galaxy y android 2.3.3 call gets disconnected automatically just after initiating. String number=Utils.getNumber(this, "contact"+i); Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + number)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TAS...


android - Placing GSM calls keeps disconnecting emulator from ADB

I am currently working on an Android project but I have a problem. I'm using Android Studio and the Android Emulator and every time I place a GSM call to the emulator, ADB loses connection to the device and I have to restart ADB to get the device detected again. The call does get placed to the emulator though. It's causing me problems as I am trying to debug a bit of code that I have written that is fired...


android - Saving socket.io client on phonegap resume or disconnecting it on pause

I have a phonegap application which uses socket.io as means of communication. When application goes to pause the socket is kept alive in the background apparently, and on resume when the page loads it replaces the old socket with a new one, and creates a new socket. By each resume socket connections are one more. Is there a way to keep the old socket connection when application resumes? Or to disconnect it when the...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top