How to set the keepalive timeout in Android?

前端 未结 3 524
时光说笑
时光说笑 2020-12-18 02:50

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.setKeepAliv

3条回答
  •  悲&欢浪女
    2020-12-18 03:42

    Android is based on Linux, and Linux supports the TCP_KEEPIDLE and TCP_KEEPINTVL socket options via the setsocketopt() function, which is wrapped by the java.net.SocketOptions interface. java.net.SocketImpl implements SocketOptions, and java.net.Socket wraps a SocketImpl. What I don't know is whether it is possible to access the SocketImpl of a given Socket object.

    What you could try doing is use Socket.setSocketImplFactory() to implement your own custom SocketImplFactory class, which is responsible for creating SocketImpl instances for Socket objects. That way, your factory could call SocketOptions.setOption() for TCP_KEEPIDLE and TCP_KEEPINTVL for any sockets your app creates.

提交回复
热议问题