且构网

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

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

更新时间:2023-12-04 15:29:28

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

通常情况下,保活时间(net.inet.tcp.keepidle)默认为 7200 秒.我不知道在 iOS 中是否如此,但是不少于 2 小时"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).