我正在创建一个类似于 WiTap 的应用程序(但要连接许多设备),这是我的问题:应用程序似乎连接了设备(它们显示在表视图控制器中。代表NSInput/OutputStream
说两个流都已打开,但是当我发送数据包时比NSOutputStream
对面设备上的函数没有NSInputStream
被调用(NSStreamEventHasBytesAvailible
。- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode)
这是进行连接以及发送和接收的代码......
//Service was resolved
- (void)netServiceDidResolveAddress:(NSNetService *)netService
{
//Get the streams from NSNetService
NSInputStream *inStream = nil;
NSOutputStream *outStream = nil;
[netService getInputStream:&inStream outputStream:&outStream];
[inStream setDelegate:self];
[inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[inStream open];
[outStream setDelegate:self];
[outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[outStream open];
[self.inStreams addObject:inStream];
[self.outStreams addObject:outStream];
//Add NSNetService between connected devices
[self.resolvedServices addObject:netService];
[self updateUI];
}
//event handling
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventHasBytesAvailable:
{
NSMutableData *inBuffer = [[NSMutableData alloc] init];
NSInputStream *inStream = (NSInputStream *)aStream;
uint8_t buffer[3];
NSInteger len = 0;
NSInteger packetNumber = 0;
len = [inStream read:buffer maxLength:3];
if(len > 0) {
[inBuffer appendBytes:buffer length:len];
[inBuffer getBytes:&packetNumber length:len];
[self.delegate stream:inStream didReceivePacketNumber:[NSNumber numberWithInteger:packetNumber]];
}
}
break;
case NSStreamEventHasSpaceAvailable:
if ([aStream isKindOfClass:[NSOutputStream class]])
NSLog(@"NSStream \"%@\" has space availible",aStream);
break;
case NSStreamEventOpenCompleted:
if ([aStream isKindOfClass:[NSInputStream class]])
NSLog(@"NSInputStream opened");
else if ([aStream isKindOfClass:[NSOutputStream class]])
NSLog(@"NSOutputStream opened");
break;
case NSStreamEventErrorOccurred:
NSLog(@"NSStream \"%@\" occurred an error: %@",aStream,[aStream streamError]);
break;
case NSStreamEventEndEncountered:
NSLog(@"Stream \"%@\" has been closed",aStream);
[aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[aStream close];
NSUInteger index = 0;
if ([aStream isKindOfClass:[NSOutputStream class]]) {
index = [self.outStreams indexOfObject:aStream];
[self.outStreams removeObject:aStream];
[self.inStreams removeObjectAtIndex:index];
} else if ([aStream isKindOfClass:[NSInputStream class]]) {
index = [self.inStreams indexOfObject:aStream];
[self.inStreams removeObject:aStream];
[self.outStreams removeObjectAtIndex:index];
}
[self.availibleServices removeObjectAtIndex:index];
[self.resolvedServices removeObjectAtIndex:index];
break;
default:
break;
}
}
//sending
//Data pushing into NSStreams
- (void)sendData:(NSData *)data toStream:(NSOutputStream *)aStream
{
if ([aStream hasSpaceAvailable]) {
NSLog(@"%i",[aStream write:[data bytes] maxLength:[data length]]);
}
}
我尝试使用 Instruments(网络事物)调试项目。它说数据包已发送和接收,但该事件从未发生。谢谢您的帮助。如果有人想要,我可以毫无问题地发送整个项目......