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.
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.
I think it might be quite important to be able to set the keepalive timeouts on an per app level, especially on a mobile device, because it might be under bad network conditions (wifi/mobile). If the app does not send (m)any data but uses a persistent connection, the socket will not detect whether the connection is lost, unless it sends tcp keepalive probes. Setting this option is usually possible via the setsockopt(2) call, but the android sdk provides only the
setKeepAlive(boolean)
option. Deeper in the stack, that functions calls libcore.io.ForwardingOs.setsockoptInt(...), which is not available directly, nor the required file descriptor. By using java reflection, setting the keepalive timeouts is possible anyway, e.g like this:This works at least until the following requirements are met:
libcore.io.ForwardingOs.setsockoptInt/4
exists at current sdk versionjava.net.Socket
has animpl
member at the current sdk versionjava.net.Socket->impl
is instance ofjava.net.SocketImpl
at the current sdk versionjava.net.SocketImpl
has afd
member at the current sdk versionTCP_KEEPIDLE
,TCP_KEEPINTVL
andTCP_KEEPCNT
have the same values (4
,5
and6
) at the current sdk version and all android devices / architectures.That seems to be true at least for android versions from 4.0.1 / November 2011 up to recent version 5.1.1 r9.
See
luni/src/main/java/libcore/io/Os.java
,luni/src/main/java/java/net/Socket.java
andluni/src/main/java/java/net/SocketImpl.java
from the platform/libcore repository.TCP_KEEPIDLE
,TCP_KEEPINTVL
andTCP_KEEPCNT
seem to have the same values for android versions since 2.2.3 r2 and all architectures. This can be validated e.g. by executingfind . -name tcp.h | xargs grep -ho "TCP_KEEP\w\+\s\+\d\+" | sort | uniq -c
in the android platform/ndk repository.Android is based on Linux, and Linux supports the
TCP_KEEPIDLE
andTCP_KEEPINTVL
socket options via thesetsocketopt()
function, which is wrapped by thejava.net.SocketOptions
interface.java.net.SocketImpl
implementsSocketOptions
, andjava.net.Socket
wraps aSocketImpl
. What I don't know is whether it is possible to access theSocketImpl
of a givenSocket
object.What you could try doing is use
Socket.setSocketImplFactory()
to implement your own customSocketImplFactory
class, which is responsible for creatingSocketImpl
instances forSocket
objects. That way, your factory could callSocketOptions.setOption()
forTCP_KEEPIDLE
andTCP_KEEPINTVL
for any sockets your app creates.