views:

58

answers:

1

Hi,

I read data from a TCP socket connection, but the server sends me \0 from time to time (which would mark the end of a message). Thus, I do not get the rest of the message.

I read like this:

uint8_t buf[tcpBufferSize];
unsigned int len = 0;

len = [inputStream read:buf maxLength:tcpBufferSize];

if(len > 0) {
    NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
    [data appendBytes: (const void *)buf length:len];
    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    //here s will be only the part before the \0

    [s release];
    [data release];
}

for example, if the server sends abc\0de y will only get abc since the \0 marks the end.

How could I receive the whole message??

+3  A: 

You most likely have received the entire message. A string is for holding text, not byte data. If you want to keep all the data including the null character, you need to leave it as an NSData object. If you want to extract certain bytes and interpret them as text, you'll have to implement that yourself.

dreamlax