0

I am using the following options to set TCP keep_alive properties on my socket running on Android connected to server running Linux.

int idleTimeout = 3;
setsockopt(socketDescriptor, SOL_TCP, TCP_KEEPIDLE, &idleTimeout, sizeof(int))
int interval = 3;
setsockopt(socketDescriptor, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(int))
int unackPacketCount = 5;
setsockopt(socketDescriptor, SOL_TCP, TCP_KEEPCNT, &unackPacketCount, sizeof(int))

The Linux server also has keep alive settings turned on in the same fashion.

Question:
Everything works as expected. But if I leave my Android device connected for like 6 to 7 hours, I get a connection timed out error. This could happen after 10 hours or after 3-4 hours. Its really random.

Is this expected because my client is an app running on Android? (Because, I don't see the issue happen when I ran my client on Linux with the same settings) Couldn't my Android socket stay connected forever without timing out?

Environment:
- Running on Android 10 on a Pixel XL
- My Android phone is always connected to charger during the test
- Its a single router between my Android device and the Linux server.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • While it would help to know what device/version of Android, connected to external power or not, and your network setup (single router, open internet). Android is a battery constrained device where behaviors like [Doze Mode](https://developer.android.com/training/monitoring-device-state/doze-standby) constrain network usage to save on battery life. – Morrison Chang Dec 09 '19 at 10:00
  • I updated the Android version and device details on the question now – TheWaterProgrammer Dec 09 '19 at 10:02
  • How are you testing the network? Most probably a single router over WiFi between Android and the Linux server but just to be sure. – Morrison Chang Dec 09 '19 at 10:03
  • Its a single router between my Android device and the Linux server. All the way through the test, I am connected to the charger. Can Android still detect my app is not getting real user events and try to do some kind of optimisation which could affect the keep alive packets transaction? – TheWaterProgrammer Dec 09 '19 at 10:04
  • 1
    Personally I would suspect as much. You don't mention how you are keeping the app alive (presumably foreground service) - but given all of the power management features like [App Buckets](https://developer.android.com/about/versions/pie/power.html#buckets) I would at least check/follow [Minimal android foreground service killed on high-end phone](https://stackoverflow.com/q/49637967/295004). Regardless you may still need to have reconnect logic similar to GCM in [Google I/O 2010 - Building push applications for Android](https://youtu.be/PLM4LajwDVc?t=665) – Morrison Chang Dec 09 '19 at 10:27
  • My app does not have a foreground service. Its a simple app with one Activity which spawns a worker Thread doing the TCP reads. Thats it. While testing I have set it to never turn off display while charging. Thanks for sharing the app buckets information on https://developer.android.com/about/versions/pie/power.html#buckets. Let me take a look if I can get affected by any of those rules. – TheWaterProgrammer Dec 09 '19 at 10:31

0 Answers0