[英]NSInputStream read returns unsigned integer maximum value when bytes available
I try to read and write data from/to Socket with NSStream. 我尝试使用NSStream从/向Socket读取和写入数据。 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. [NSStream read:maxLength:]
始终返回最大无符号整数值。 Eventually i get this error: 最终我收到此错误:
Fail: *** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (4294967295)
Why does read mehod return this big value? 为什么阅读mehod会带来这么大的价值? Does it really read that much bytes? 它真的读了那么多字节吗? (I don't think so) :) (我不这么认为):)
PS: Socket Stream server is ok. PS:Socket Stream服务器还可以。 it reads and writes data to other clients also and there is no problem. 它也可以读取和写入其他客户端的数据,没有问题。
from the NSInputStream read:maxLength documentation: 从NSInputStream中读取:maxLength文档:
Return Value A number indicating the outcome of the operation A positive number indicates the number of bytes read 0 indicates that the end of the buffer was reached A negative number means that the operation failed 返回值表示操作结果的数字正数表示读取的字节数0表示已达到缓冲区的末尾负数表示操作失败
so in case of end of stream your len is 0, in case of error it is -1 which explains the 4294967295 value on your unsigned int. 因此,在流结束的情况下,len为0,如果出现错误,则为-1,这解释了unsigned int上的4294967295值。
so use a signed int and check for negative values. 所以使用signed int并检查负值。
I resolved the problem. 我解决了这个问题。 I was writing data without observing if has space available in output stream. 我在没有观察输出流中是否有可用空间的情况下编写数据。
If from CFReadStreamRead()
method returns 1, means the request fails, you should do the processing of failure. 如果从CFReadStreamRead()
方法返回1,表示请求失败,则应该进行失败处理。 CFReadStreamRead()
method to read failure will return 1, with 4294967295-1 is the same block of memory, so the length was 4294967295. 读取失败的CFReadStreamRead()
方法将返回1,与4294967295-1是同一块内存,因此长度为4294967295。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.