简体   繁体   English

真正的iPhone上的NSInputStream问题

[英]problem with NSInputStream on real iPhone

I have a problem with NSInputStream. 我对NSInputStream有问题。 Here is my code: 这是我的代码:

case NSStreamEventHasBytesAvailable:

        printf("BYTE AVAILABLE\n");

        int len = 0;
        NSMutableData *data = [[NSMutableData alloc] init];

        uint8_t buffer[32768];

        if(stream == iStream)
        {       
            printf("Receiving...\n");                       

            len = [iStream read:buffer maxLength:32768];                

            [data appendBytes:buffer length:len];                   

        } 
        [iStream close];

I try to read small data and it works perfectly on simulator and real iPhone. 我尝试读取小数据,并且它在模拟器和真实的iPhone上都可以完美运行。 If I try to read large data (more than 4kB or maybe 5kB), the real iPhone just can read 2736 bytes and stop. 如果我尝试读取大数据(大于4kB或5kB),那么真正的iPhone只能读取2736字节并停止。

Why is it? 为什么? Help me plz! 请帮我! Merci d'avance! 谢谢你!

Your data object needs to be external to your stream handler. 您的数据对象必须在流处理程序外部。 It is often the case that when large abounts of data are coming in, you get it in chunks and not all at once. 通常情况是,当大量数据涌入时,您会成块而不是一次全部获得数据。 Just keep appending data to it until you receive bytesRead == 0; 只是继续向其添加数据,直到收到bytesRead == 0;。 Then you can close your stream and use the data. 然后,您可以关闭流并使用数据。

   case NSStreamEventHasBytesAvailable: {
        NSInteger       bytesRead;
        uint8_t         buffer[32768];

        // Pull some data off the network.

        bytesRead = [self._networkStream read:buffer maxLength:sizeof(buffer)];

        if (bytesRead == -1) {
            [self _stopReceiveWithFailure];
        } else if (bytesRead == 0) {
            [self _stopReceiveWithSuccess];
        } else {
            [data appendBytes:buffer length:len];                   
        }

看起来您每次都在创建一个新的data对象……也许您应该将其创建并保留为一个属性,然后像上面一样附加到该对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM