简体   繁体   English

NSInputStream读取:maxlength:返回的字节数比maxlength多

[英]NSInputStream read: maxlength: returning way more bytes than maxlength

I have an iOS app where I am using NSInputStream (based on a CFReadStreamRef) for reading from a network socket. 我有一个iOS应用程序,其中使用NSInputStream(基于CFReadStreamRef)从网络套接字读取。 I keep getting data from the server and I keep reading and processing it (using read:bytesBuffer maxLength:l ). 我不断从服务器获取数据,并继续读取和处理数据(使用read:bytesBuffer maxLength:l )。 It works fine the first several times but on about the 20-25th read, this method reports that it read a HUGE number of bytes, eg 4,294,967,295 bytes when I really asked for a max of say 1-3MBytes . 它在前几次工作正常,但是在读取20-25时,此方法报告它读取了巨大的字节数,例如,当我真正要求最大1-3MBytes时,读取了4,294,967,295字节。 This is bizarre and seems like a bug in NSInputStream/CFReadStream API. 这很奇怪,似乎是NSInputStream / CFReadStream API中的错误。

My app eventually crashes as it tries to load all these bytes into a buffer that is not allocated for the number of bytes returned (and the server is not returning this many bytes in the first place!) 我的应用最终尝试将所有这些字节加载到未分配用于返回的字节数的缓冲区中时崩溃(并且服务器首先没有返回这么多字节!)

Has anyone encountered this issue before? 有人遇到过这个问题吗?

Thanks! 谢谢!

The return value from read:maxLength: is an NSInteger which will be negative on failure. read:maxLength:的返回值是一个NSInteger ,失败时将为负。 You probably are casting it to an unsigned integer type which will turn negative numbers into huge positive numbers. 您可能将其转换为无符号整数类型,它将把负数变成巨大的正数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSInputStream read:maxLength:无法读取数据,返回-1 - NSInputStream read:maxLength: cannot read data, returns -1 当-[NSInputStream read:maxLength:]返回0时,它在Apple Document中是否模棱两可? - Is it ambiguous in Apple Document when -[NSInputStream read:maxLength:] return 0? - [NSInputStream read:maxLength:]抛出一个异常,说长度太大,但事实并非如此 - -[NSInputStream read:maxLength:] throws an exception saying length is too big, but it isn't NSInputStream:read:maxLength:在TCP套接字流上返回错误“操作无法完成。 错误的文件描述符” - NSInputStream:read:maxLength: on a TCP socket stream returns the error “The operation couldn’t be completed. Bad file descriptor” 当可用字节时,NSInputStream读取返回无符号整数最大值 - NSInputStream read returns unsigned integer maximum value when bytes available 将maxlength设置为特定的UITextField - Set maxlength to specific UITextField 如何将NSInputStream转换为NSString或如何读取NSInputStream - How to convert NSInputStream to NSString or how to read NSInputStream [__NSArrayM getFileSystemRepresentation:maxLength:]:发送到实例的无法识别的选择器 - [__NSArrayM getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 如何使用NSString getBytes:maxLength:usedLength:encoding:options:range:remainingRange: - How to use NSString getBytes:maxLength:usedLength:encoding:options:range:remainingRange: 将char []写入NSOutputStream并从NSInputStream读取 - write char[] to NSOutputStream and read from NSInputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM