基于TCP的客户端通信不';你不能在iOS 11中工作吗?
我有一个用于与设备通信的物联网应用程序。在iOS 11发布之前一切正常。它在iOS 11及以上版本中不通信,但在早期版本(最多10个)中可以工作。这是代码 建立套接字连接基于TCP的客户端通信不';你不能在iOS 11中工作吗?,ios,objective-c,sockets,ios11,iot,Ios,Objective C,Sockets,Ios11,Iot,我有一个用于与设备通信的物联网应用程序。在iOS 11发布之前一切正常。它在iOS 11及以上版本中不通信,但在早期版本(最多10个)中可以工作。这是代码 建立套接字连接 (void)setUpSocketConnection { @try { CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) VeranoHost,VeranoPort , &readStr
(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;
}
}
因此,当连接到插座时,它会进入
nsstreamventerrorocurred
在事件处理程序和日志中-操作无法完成。没有到主机的路由
任何帮助都将不胜感激
2017年12月19日更新
- 获得了一个允许发现主机地址的包装器,我可以确认它返回NetworkStatus
可通过WiFi访问
- 套接字通信工作正常,我尝试创建一个示例服务器套接字,能够发送和接收数据
USR-WIFI232-S
低功耗WiFi模块
用户手册
更新日期2017年12月20日(2)
Eurekaa,但该解决方案无法实时应用。在iOS 11中设置与IoT模块主机地址对应的最接近的静态IP地址范围时,通信工作正常。但它不适用于动态分配的IP地址。我想知道它是否适用于应用程序传输安全(ATS),请参阅: 对于iOS 11 ATS更新,一些ATS更新预计将作为其中的一部分:
- TLSv1.3将获得初步支持
- 3DE将从批准的密码列表中删除
- 使用SHA1签署的证书将不再被接受
- 使用RSA密钥签名的证书必须具有2048位或更大的密钥长度
<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>
NSAppTransportSecurity
NSAllowsArbitraryLoads
NSExceptionDomains
creativecommons.org
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
N第三方例外要求转发保密
本地服务器
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
还有一个选项,如果您想禁用ATS,可以使用:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
NSAppTransportSecurity
NSAllowsArbitraryLoads
但这是不推荐的 错误消息/代码是什么?@Paulw11-
n错误*错误=[theStream Streamer error]代码>我已转换为n错误
,然后记录localizedDescription
,显示操作无法完成。没有到主机的路由
因此,它无法到达主机,因为它收到了ICMP响应,表示无法路由到主机。主机是否在本地网络上?您是否尝试过网络捕获来为ios事件添加种子以尝试连接?是的。至于为什么它在iOS11中失败,我猜这是一个bug。你试过在苹果开发者论坛上提问吗?我告诉你,iOS11是如此的笨重that@Stella. 你能试试这个吗