且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在Apple iOS设备上激活TCP keepalive

更新时间:2023-12-04 15:24:31

参见:http://en.wikipedia.org/wiki/Keepalive#TCP_keepalive

通常,保持活动时间( net.inet.tcp.keepidle )默认为7200秒。
我不知道在iOS中是否属实,但不少于2小时 / html / rfc1122#page-101> RFC 1122

Usually, keepalive time (net.inet.tcp.keepidle) is 7200sec by default. I don't know that is true in iOS or not, but "no less than 2 hours" is clearly required by RFC 1122.


此间隔必须是
可配置且必须默认为不少于两个小时。

This interval MUST be configurable and MUST default to no less than two hours.

那么,你怎么能在你的应用程序中配置它?尝试:

So, how could you configure it in your application? try:

#import <sys/types.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <netinet/tcp.h>

int on = 1;
int delay = 120;
setsockopt(socket_handle, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
setsockopt(socket_handle, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay));

参见 man tcp

我确认这适用于iOS模拟器(iPhone 5s / iOS 8.0)。

I confirmed this works on iOS Simulator (iPhone 5s / iOS 8.0).