Reputation: 13
Error appear when i received data from server (data just string).
for example
server sent data (15000bytes)
my iPhone received data (7878bytes)
so I try to searching and i know while method is answer.
but I can't apply my code..
I guess NSMutableData is hint..
how to do ..
Here my code.
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@"stream event %lu", (unsigned long)streamEvent);
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
if(_connectServer)
{
[_connectServer invalidate];
_connectServer = nil;
}
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream)
{
if(!_serverOpen)
{
uint8_t buffer[1024];
int len;
while ([inputStream hasBytesAvailable])
{
len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
if (nil != output)
{
[self receivedServerData:output];
if(!_serverOpen)
{
[self initNetworkCommunicationWithIpPort:output];
}
}
}
}
}
else
{
uint8_t buffer[20];
int len;
while ([inputStream hasBytesAvailable])
{
len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
NSString* strBuffer = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
NSLog(@"STRBUFFER %@", strBuffer);
strBuffer = [strBuffer substringFromIndex:7];
uint8_t dataBuffer[[strBuffer intValue]];
NSLog(@"databuffer %lu", sizeof(dataBuffer));
int lenBuffer = [inputStream read:dataBuffer maxLength:sizeof(dataBuffer)];
NSLog(@"lenBuffer %d", lenBuffer);
int position = 0 ;
if (lenBuffer > 0)
{
NSString *output = [[NSString alloc] initWithBytes:dataBuffer length:lenBuffer encoding:NSUTF8StringEncoding];
if (nil != output)
{
[self receivedServerData:output];
if(!_serverOpen)
{
[self initNetworkCommunicationWithIpPort:output];
}
}
}
}
}
}
else
{
NSLog(@"333");
}
break;
case NSStreamEventHasSpaceAvailable:
{
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
_serverOpen = NO;
break;
case NSStreamEventEndEncountered:
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
theStream = nil;
break;
default:
NSLog(@"Unknown event");
}
}
Upvotes: 0
Views: 516
Reputation: 112857
As @Manav states you can expect to receive the stream in several chunks, just append each to the NSMutableData until you get an NSStreamEventEndEncountered
.
Upvotes: 1
Reputation: 997
If you have access to the server which is sending the data the easiest way to do this would be to make the first 4 bytes an int
which represents the length of the data. Then you parse the first 4 bytes in the data and read the length. Then once you have the length when there's space available on the input stream keep appending the bytes to an NSMutableData
property until the receiver has length
that you got in the beginning. Once it does, then convert it to the string at once. Due to network latency issues you cannot be sure that all the data will be received on the stream at once.
Upvotes: 0