Search code examples
objective-ciosnsdatansstream

NSInputStream read returns unsigned integer maximum value when bytes available


I try to read and write data from/to Socket with NSStream. Here is my code for connect :

- (void)connect
{
  [NSStream getStreamsToHostNamed:APIC_HOST_ADDR 
                             port:APIC_HOST_PORT
                      inputStream:&inStream
                     outputStream:&outStream];
  [inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  [outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

  inStream.delegate = self;
  outStream.delegate = self;

  if ([inStream streamStatus] == NSStreamStatusNotOpen)
    [inStream open];

  if ([outStream streamStatus] == NSStreamStatusNotOpen)
    [outStream open];

}

and for input stream i implement the delegate methods to recieve events

- (void)handleInputStreamEvent:(NSStreamEvent)eventCode
{
  switch (eventCode) {
    case NSStreamEventHasBytesAvailable:
    {
    int bytesRead;
    if (data == nil) {
      data = [[NSMutableData alloc] init];
    }
    uint8_t buf[1024];
    unsigned int len = 0;
    len = [inStream read:buf maxLength:1024];
    if(len>0) {
      @try {
        [data appendBytes:(const void *)buf length:len];
      }
      @catch (NSException *exception) {
        NSLog(@"Fail: %@", exception);
      }
      @finally {
        NSLog(@"Finally");
        bytesRead += len;
      }
    } else {
      NSLog(@"No Buffer");
    }  

    NSString *str = [[NSString alloc] initWithData:data 
                                          encoding:NSUTF8StringEncoding];
    NSLog(@"%@",str);
    [str release];
    [data release];        
    data = nil;
    } break;
    case NSStreamEventErrorOccurred:
    {
      NSError *theError = [inStream streamError];
      NSLog(@"Error reading stream! ,Error %i: %@",[theError code], [theError localizedDescription]);
      [self disconnect];
      [self connect];
    } break;
  }
}

[NSStream read:maxLength:] always returns maximum unsigned integer value. Eventually i get this error:

Fail: *** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (4294967295)

Why does read mehod return this big value? Does it really read that much bytes? (I don't think so) :)

PS: Socket Stream server is ok. it reads and writes data to other clients also and there is no problem.


Solution

  • I resolved the problem. I was writing data without observing if has space available in output stream.