作者:zhenhuaYang | 来源:互联网 | 2023-01-04 17:02
我有一个使用Socket编程与设备通信的IoT应用程序.一切顺利,直到iOS 11发布.它在iOS 11及更高版本中不通信,但它适用于早期版本(最多10个).这是代码.
建立套接字连接
(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包装器,允许发现主机地址,我可以确认它返回NetworkStatusReachableViaWiFi
套接字通信工作正常,我尝试通过创建示例服务器套接字,能够发送和接收数据.
2017年12月20日更新(1)
物联网设备详细信息 - USR-WIFI232-S
低功耗WiFi模块
用户手册在这里
2017年12月20日更新(2)
Eurekaa,但解决方案无法实时应用.在iOS 11中设置与IoT模块的主机地址对应的最近静态IP地址范围时,通信正常.但它不适用于动态分配的IP地址.