Keeping a socket connection alive in iOS

拈花ヽ惹草 提交于 2019-11-27 21:31:41

The simplest answer is to try to use the SO_KEEPALIVE socket option.

Broken connections can be hard to detect without data flowing between the end-points which is what makes this option in some ways useless as it doesn't use data to detect broken connections. However, it is easy to add to your code to see. Add it and see if it helps...

This is how it's done in C or C++

int opt = 1;
// Get the native socket handle from your socket class here...
int socket_handle = getNativeSocketHandle( self.outputStream );

/*Enabling keep alive*/
if( setsockopt( socket_handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof( opt ) ) < 0 )
{
   // failed setting socket option
}

The not-so-simple answer is to add a ping packet to your protocol and send that ping packet regularly so you can detect a broken connection.

I found how to do this. Since the server is running node.js, I used

socket.setKeepAlive(true, 2000);

And in this way node keeps each socket open. The default is false, so this is why it was timing out. I hope this is useful to other people. Thank your for your responses, I think keeping a heartbeat (keep-alive) is also a good idea, but with this native approach node takes care of it.

Assuming a NSOutputStream *oStream;

On iOS do it this way:

#if 1 // Using CF
CFDataRef data = (CFDataRef)CFWriteStreamCopyProperty((__bridge CFWriteStreamRef)oStream, kCFStreamPropertySocketNativeHandle);
if(data) {
    CFSocketNativeHandle socket_handle = *(CFSocketNativeHandle *)CFDataGetBytePtr(data);
    CFRelease(data);
#else // Using Foundation
NSData *data = (NSData *)[oStream propertyForKey:(__bridge NSString *)kCFStreamPropertySocketNativeHandle];
if(data) {
    CFSocketNativeHandle socket_handle = *(CFSocketNativeHandle *)[data bytes];
#endif
    NSLog(@"SOCK HANDLE: %x", socket_handle);

    /*Enabling keep alive*/
    if( setsockopt( socket_handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof( opt ) ) < 0 )
    {
       NSLog(@"Yikes 2: failed to set keepalive! ERRNO: %s", strerror(errno));
    }

I'm posting this since I just spent hours working it all out, want to save someone else the effort.

Linuxios

You need to routinely send some garbage over the connection. Try this:

NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:240 target:self selector:@selector(timerMethod:) userInfo:[NSNumber numberWithInt:sockfd] repeats:YES];

Then declare this in self:

-(void)timerMethod:(NSTimer*)t
{
  send([[t userInfo] intValue], "Keepalive!", 11, 0);
}

Note: Replace `"Keepalive!" with whatever you want for your protocol. The only requirement is that the remote end (socket server) ignores the message.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!