NSInputStream read:maxBytes always returns 0
I'm baffled. I have an open NSInputStream which thinks it has bytes available. When I read the bytes, the operation always returns 0. I've searched everywhere and my code looks like most everyone else's doing the same thing. This is such a low level operation 开发者_如何转开发that I can't figure out how it could go wrong. I've tried this by connecting to a number of different hosts. I've also watched with Wireshark and I can see the host receiving the bytes I write, but the read:maxBytes operation still always returns 0???
case NSStreamEventHasBytesAvailable:
{
//Check stream status
NSString *returnedStatus;
commandLength = [commandString length];
[commandString deleteCharactersInRange:NSMakeRange(0, commandLength)];
returnedStatus = [NSString stringWithString:[self decodeStatus:[stream streamStatus]]];
[commandString appendFormat:@"inputStream %@ status=%@\n",stream, returnedStatus];
[self writeCommand:commandString];
uint8_t *buf[buffLength];
NSUInteger len = 0;
len = [(NSInputStream *)stream read:(uint8_t *)buf maxLength:buffLength];
if (len == 0) {
//len = buffLength;
[self writeCommand:@"No bytes read!\n"];
}
I also run into this issue and i found an answer in the documentation:
- (BOOL)hasBytesAvailable
Return Value:
YES if the receiver has bytes available to read, otherwise NO. May also return YES if a read must be attempted in order to determine the availability of bytes.
精彩评论