Question

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!");
Was it helpful?

Solution 2

TCP streams are just that, byte streams, they are inherently not message oriented and individual network packets may be reorganized, split, and merged at will. The only thing that is guaranteed from end-to-end is that all of the bytes will arrive in the order they were sent. You'll need to handle buffering partial lines (or more frequently combined lines) yourself by splitting them out on newlines and saving partial results.

OTHER TIPS

Well what I ended up doing was just searching for the end of line character myself and building processing it.

-(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) {
                         if ([output rangeOfString:@"\n"].location == NSNotFound) {

                            line = AS(line, output);

                        } else {



                            NSArray *chunks = [output componentsSeparatedByString: @"\n"];

                            line = AS(line, chunks[0]);
                            [self messageReceived:line];
                            DLog("M: %@", line);

                            line = chunks[1];


                        }



                    }
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top