I am currently working on a client on iOS for a TCP server. The TCP server works totally fine, I can connect to it with any other client (for example Telnet) and read and receive messages. When one connects for example, it is supposed to send "dh:1000:1" (the numbers might change). If you try it out on Telnet etc. it works. However, I am having difficulties to read that string in XCode. I read from the TCP server the following way:
// -(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
// etc.....
if (theStream == inputStream) {
uint8_t buffer[1024];
int length;
while ([inputStream hasBytesAvailable]) {
length = [inputStream read:buffer maxLength:sizeof(buffer)];
if (length > 0) {
NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
[data appendBytes: (const void *)buffer length:length];
NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
if (nil != output) {
[self messageReceived:output];
}
}
}
}
When I use the debugger, the NSString $output$ is ALWAYS an empty string. Interestingly enough though, the $buffer$ does contain the data. If I use the Xcode debugger, I can see the following:
You can clearly see that the "dh:1:0..." is there, so the problem is somewhere in this line:
NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
But I cannot figure it out :/