5

我编写了这段代码来设置与服务器的流:

-(void)streamOpenWithIp:(NSString *)ip withPortNumber:(int)portNumber;
{
       CFReadStreamRef readStream;
       CFWriteStreamRef writeStream;
       CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)ip, portNumber, &readStream, &writeStream);

       if(readStream && writeStream)
       {
            CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
            CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

            //Setup inpustream
            inputStream = (__bridge NSInputStream *)readStream;
            [inputStream setDelegate:self];
            [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [inputStream open];

            //Setup outputstream
            outputStream = (__bridge NSOutputStream *)writeStream;
            [outputStream setDelegate:self];
            [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [outputStream open];
       }
}

我能够连接到服务器并发送和接收数据。但我想检查连接是否仍然存在。如果我从 wifi 路由器上断开电缆,我仍然可以在流中发送数据并且没有发生错误。

您可以在应用程序级别解决此问题,方法是使用计时器发送一些消息并检查您是否收到回复。但是您也可以在较低级别使用 TCP Keep-Alive 技术解决此问题。

我如何用 NSStream 实现这个?如何设置检查间隔?

我假设您在流关闭时通过 TCP Keep-Alive 检查得到 NSStreamEventErrorOcurred?

我已经检查了这篇文章,但我无法弄清楚: 在 iOS 中保持套接字连接处于活动状态

谢谢你的帮助!

4

2 回答 2

12

您可以使用以下方式获取本机套接字句柄

CFDataRef socketData = CFReadStreamCopyProperty((__bridge CFReadStreamRef)(inputStream), kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle socket;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), (UInt8 *)&socket);
CFRelease(socketData);

然后设置套接字选项(你需要这样做#include <sys/socket.h>):

int on = 1;
if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1) {
   NSLog(@"setsockopt failed: %s", strerror(errno));
}

您可以将此代码放在事件的事件处理函数中kCFStreamEventOpenCompleted

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event {
    switch (event) {
        case kCFStreamEventOpenCompleted:
            if (stream == self.inputStream) {
                CFDataRef socketData = CFReadStreamCopyProperty((__bridge CFReadStreamRef)(stream), kCFStreamPropertySocketNativeHandle);
                CFSocketNativeHandle socket;
                CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), (UInt8 *)&socket);
                CFRelease(socketData);

                int on = 1;
                if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) == -1) {
                    NSLog(@"setsockopt failed: %s", strerror(errno));
                }
            }
            break;

        /* ... other cases ... */;
    }
}
于 2013-04-03T12:14:05.480 回答
0

类似问题有更完整的答案。

对于在 10 秒后开始发送 keepalive、每 2 秒发送一次keppalive 并在 4 次 keepalive 后无回复关闭流的应用示例,请查看此帖子: Is it possible to activate TCP keepalive on Apple iOS devices

它还显示了如何设置重新传输超时,然后关闭连接。

于 2015-12-15T15:22:27.563 回答