Problem with NSInputStream on a real iPhone

I have a problem with 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 great on a simulator and a real iPhone. If I try to read big data (more than 4 KB or maybe 5 KB), a real iPhone can just read 2736 bytes and stop.

Why? Help me plz! Merci d'avance!

+4
source share
2 answers

Your data object must be external to your stream handler. It often happens that when large amounts of data appear, you get them in pieces, not immediately. Just keep adding data to it until you get 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]; } 
+2
source

It seems that every time you create a new data object ... maybe you should create and save it as a property and add to it since you are higher.

0
source

Source: https://habr.com/ru/post/1298987/


All Articles