I am using the following code to read from a server socket. Everything starts out fine, but as things progress the data returned gets truncated and chunked incorrectly. I've read all over trying different things like changing the buffer size and synchronizing the code, but still no luck. At first I thought it was due
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
getting called asynchronously so I tried synchronizing it, but no luck there. I increased the buffer several times and no luck... I just want one whole line read from the server passed to the messagedReceived each time it fires it. I am sure I am doing something stupid or overlooking something here that will be obvious to someone.
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
DLog(@"Connection Opened!!!");
[delegate connectionOpened];
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
int len = 0;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
DLog(@"%@", output);
[self messageReceived:output];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
DLog(@"Can not connect to the host!");