首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于TCP的客户端通信在iOS 11中不起作用?

基于TCP的客户端通信在iOS 11中不起作用?
EN

Stack Overflow用户
提问于 2017-12-18 09:57:40
回答 1查看 1K关注 0票数 15

我有一个IoT应用程序,它使用套接字编程与设备通信。在iOS 11发布之前,一切都很好。它在iOS 11和更高版本中不通信,但在早期版本(最多10)中工作。这是密码。

建立套接字连接

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

开放流

代码语言:javascript
复制
(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];
}

写入输出流

代码语言:javascript
复制
(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];

}

事件处理程序

代码语言:javascript
复制
(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;
    }
}

因此,当连接到套接字时,它在事件处理程序和日志- The operation couldn’t be completed. No route to host中输入到The operation couldn’t be completed. No route to host

任何帮助都将不胜感激。

更新2017年12月19日

  • 获得一个可达性包装器,它允许发现主机地址,并且我可以确认它返回NetworkStatus ReachableViaWiFi
  • 套接字通信工作良好,我尝试通过创建一个示例服务器套接字,能够发送和接收数据。

更新2017年12月20日(1)

IoT设备详细信息- USR-WIFI232-S低功耗WiFi模块 用户手册这里

更新2017年12月20日(2)

Eurekaa,但解决方案不能实时应用。在iOS 11中设置与IoT模块的主机地址相对应的最接近的静态IP地址范围时,通信工作良好。但它不适用于动态分配的IP地址。

EN

回答 1

Stack Overflow用户

发布于 2018-01-21 05:53:18

如果it应用程序传输安全(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将获得初步支持
  • 3 3DES将从批准的密码列表中删除。
  • 用SHA1签署的证书将不再被接受
  • 用RSA密钥签名的证书必须具有2048位密钥长度或更大的长度。

您在应用程序中使用的PLIST和应享权利?

代码语言:javascript
复制
<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,可以使用以下选项:

代码语言:javascript
复制
<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>

但这是不推荐的!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47865874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档