如何在Android中设置keepalive超时? [英] How to set the keepalive timeout in Android?
问题描述
我想将我打开的套接字上的 TCP 保持连接时间从 2 小时降低到大约 10 分钟.我可以让它通过 socket.setKeepAlive(true) 使用 keepalive,但是如何控制发送 keepalive 数据包之前的时间?
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?
如果我使用 NDK,我似乎可以做到这一点,但我想将此代码作为 jar 分发,所以这对我来说并不理想.
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.
推荐答案
这可能是一个太明显的答案 [即这不是针对您的特定情况的选项]但您当然可以通过每 10 分钟发送 1 个丢弃字节(在任一方向)来实现自己的保持连接.
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.
这篇关于如何在Android中设置keepalive超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!