基于TCP的客户端通信在iOS 11中无法工作?

14
我有一个物联网应用程序,它使用Socket编程与设备通信。一切顺利直到iOS 11发布。它在iOS 11及以上版本中无法通信,但在早期版本(最高可达10)中可以工作。以下是代码。

建立Socket连接

(void)setUpSocketConnection {
    @try {
        CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) VeranoHost,VeranoPort , &readStream, &writeStream);
        [self open];
    }
    @catch (NSException *exception) {
        NSLog(@"Open Exception:%@", exception.reason);
    }   
}

打开流

(void)open {
    //NSLog(@"Opening streams.");
    _outputStream = (__bridge  NSOutputStream *)writeStream;
    _inputStream = (__bridge  NSInputStream *)readStream;
    [_outputStream setDelegate:self];
    [_inputStream setDelegate:self];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
    dispatch_async(queue, ^ {
            [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop] run];
    });

    [_outputStream open];
    [_inputStream open];

    //[self disableNaglesAlgorithmForStream:_inputStream];
   // NSLog(@"Connected");
//    self.timeOutTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
//                                                         target:self
//                                                       selector:@selector(timerTimeOutAction:)
//                                                       userInfo:nil
//                                                        repeats:NO];
}

输出流写入

(void)writeData:(NSString *)message forSocketType:(SOCKETTTYPE)socketType {
    self.socketType = socketType;
    NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]];
    [_outputStream write:[data bytes] maxLength:[data length]];
    [_outputStream close];

}

事件处理程序

(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
   // NSLog(@"stream event %lu", (unsigned long)streamEvent);
    switch (streamEvent) {
        case NSStreamEventOpenCompleted:{

           // NSLog(@"NSStreamEventOpenCompleted :::: Stream opened and connected");
        }
            break;
        case NSStreamEventHasBytesAvailable:
            // NSLog(@"NSStreamEventHasBytesAvailable :::: Stream opened and connected");
            if (theStream == _inputStream) {
                uint8_t buffer[1024];
                NSInteger len;

                while ([_inputStream hasBytesAvailable])
                {
                    len = [_inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0)
                    {
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != output)
                        {
                            NSLog(@"server said: %@", output);
                            [self messageReceived:output];
                        }
                    }
                }
            }
            break;
        case NSStreamEventHasSpaceAvailable:
           // NSLog(@"NSStreamEventHasSpaceAvailable :::: Stream has space available now");
            break;

        case NSStreamEventErrorOccurred:{
           NSError *theError = [theStream streamError];
            NSLog(@"Error Description:%@",theError.localizedDescription);
            [self close];
            if(self.delegate &&[self.delegate respondsToSelector:@selector(socketHandlerItem:failureWithError:forSocketType:)]){
                [self.delegate socketHandlerItem:self failureWithError:[theStream streamError] forSocketType:self.socketType];
            }
             //NSLog(@"NSStreamEventErrorOccurred :::: %@",[theStream streamError].localizedDescription);
        }
            break;

        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            if(self.delegate &&[self.delegate respondsToSelector:@selector(socketHandlerItem:eventStopedWithstatus:forSocketType:)]){
                [self.delegate socketHandlerItem:self eventStopedWithstatus:YES forSocketType:self.socketType];
            }
           // NSLog(@"NSStreamEventEndEncountered :::: close stream Disconnected");
            break;
        default:
           // NSLog(@"Unknown event");
            break;
    }
}

当连接到套接字时,事件处理程序会进入NSStreamEventErrorOccurred状态,并记录日志-The operation couldn't be completed. No route to host。希望能得到帮助。
2017年12月19日更新: 获得了一个Reachability包装器,可以发现主机地址,我可以确认它返回的NetworkStatus是ReachableViaWiFi。 Socket通信正常工作,我通过创建一个示例服务器套接字来尝试发送和接收数据。
2017年12月20日更新(1): IoT设备详情-USR-WIFI232-S低功耗WiFi模块 用户手册在这里 2017年12月20日更新(2): 太好了,但解决方案不能实时应用。在iOS 11中设置最近范围的静态IP地址与IoT模块的主机地址相对应,通信正常工作,但动态分配的IP地址不起作用。

@Paulw11 - NSError *theError = [theStream streamError]; 我已经将其转换为 NSError,然后记录了 localizedDescription,它显示为 操作无法完成。没有到主机的路由 - Stella
1
所以,它无法到达主机,因为它收到了一个ICMP响应,说明没有路由到它。主机是否在本地网络上?您尝试过进行网络捕获以查看iOS是否正在尝试连接吗? - Paulw11
@Paulw11,基本上它是一个物联网设备,我正在连接iPhone到该设备提供的WiFi。WiFi通信在旧版iOS中可以工作。因此,我们可以假设设备没有问题,对吗? - Stella
1
是的。至于为什么它在iOS11上失败,我猜测这是一个错误。你尝试在苹果开发者论坛上问过吗?我告诉你,iOS11非常有bug。 - GeneCode
1
@Stella。你可以试试这个:https://www.raywenderlich.com/157128/real-time-communication-streams-tutorial-ios - Vaisakh
显示剩余7条评论
1个回答

1
我想知道是否使用了应用程序传输安全性(ATS),请参见:https://www.nowsecure.com/blog/2017/08/31/security-analysts-guide-nsapptransportsecurity-nsallowsarbitraryloads-app-transport-security-ats-exceptions/ 对于iOS 11 ATS更新,预计会有一些ATS更新内容:
  • TLSv1.3将获得初步支持
  • 3DES将从密码列表中删除
  • 不再接受使用SHA1签名的证书
  • 使用RSA密钥签名的证书必须具有2048位密钥长度或更大
您的应用程序中使用的是PLIST和Entitlements吗?
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>

        <key>creativecommons.org</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>

        <key>localhost</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>

    </dict>
</dict>

另外还有一种选择,如果您想禁用ATS,可以使用以下方法:

<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>

但这并不是推荐的做法!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接