4

I am creating an app similar to WiTap (but to connect many devices) and here is my problem: The application seems to connect the devices (they are displayed in table view controller. The delegate of NSInput/OutputStream says both streams are opened but when I send packet to NSOutputStream than the function on opposite device with NSInputStream doesn't get called (NSStreamEventHasBytesAvailible in - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode).

Here is the code that is doing the connection and sending and receiving ...

//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]]);
 }
}

I tried to debug the project using Instruments (the network thing). It says packet is send and received but the event never occurs. Thank you for your help. If anyone wants I have no problem sending the whole project ...

Jack
  • 10,943
  • 13
  • 50
  • 65
ipek
  • 41
  • 4
  • 1
    can you get any other events? Did you set your delegate? – yeesterbunny Oct 16 '12 at 18:14
  • Yes delegates are set and not nill ... and the event NSStreamEventOpenCompleted gets called but not NSStreamEventHasBytesAvailible. – ipek Oct 16 '12 at 18:42
  • Where do you write your data? Where is your code for [outStream hasSpaceAvailable] and [outStream write: someData maxLength: someDataLength] – yeesterbunny Oct 16 '12 at 18:48
  • well in the last function in the NSLog (its sending and loging the amount of bytes sent at the same time). As I said Xcode instruments (the section where it says what is the network usage) is saying that the application send matching data and received on another device so I am assuming that checking whether hasSpaceAvailible is sufficient. Also it logs that it sent exact length as I wanted – ipek Oct 16 '12 at 18:54
  • hmm...okay email me your project? please include a readme of some sort so I know what to look for. If I find something I'll let you know. – yeesterbunny Oct 16 '12 at 21:17
  • can't find the PM or e-mail button so I uploaded it on mediafire http://www.mediafire.com/?y6wr5u5e4gt9ln5. There is ReadMe file inside project in supporting files. I hope it will be clear enough to familiarize yourself with my code. – ipek Oct 17 '12 at 06:07
  • I looked at your code but I couldn't find the answer to your problem. If you find out what the problem is please let me know. – yeesterbunny Oct 18 '12 at 04:02
  • ok i'm new to this thing but is it okay to just send bunch of numbers to the NSOutputStream and expect it on NSInputStream? (that's what I am doing) or do I need to give some header or something? – ipek Oct 21 '12 at 18:54

0 Answers0