Search code examples
androidsocketstcpkeep-alive

How to set the keepalive timeout in Android?


I'd like to lower the TCP keepalive time on a Socket I'm opening from 2 hours to something on the order of ten minutes. I can make it use keepalive with socket.setKeepAlive(true), but how can I control the time before a keepalive packet is sent?

It looks like I could do this if I was using the NDK, but I want to distribute this code as a jar, so that's not ideal for me.


Solution

  • This is probably too obvious an answer [i.e. it's not an option for your specific case] but you can of course implement your own keepalive by sending 1 throw-away byte (in either direction) every 10 minutes.