Disclaimer: I am an Xcode / iPhone SDK Noob.
I am trying to establish a client-side TCP/IP connection to an existing server. Upon connection, I expect to receive some data about the server (version, etc.).
When my connection is made, the NSStreamEventOpenCompleted event fires, so I know the connection is made. Next the NSStreamEventHasBytesAvailable event fires and I am executing the following code. The value 71 (int) is stored in len, which I believe is correct. However, the line
[data appendBytes:&buffer length:len];
is crashing (I think). There is no actual error thrown but I do see __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__ even though I have clearly added error catching:
case NSStreamEventHasBytesAvailable: { NSMutableData *data=[[NSMutableData alloc] init]; uint8_t *buffer[1024]; unsigned int len=0; len=[(NSInputStream *)stream read:buffer maxLength:1024]; if(len>0){ @try{ [data appendBytes:&buffer length:len]; } @catch(NSException *ex){ NSLog(@'Fail: %@', ex); } [statusLabel setText:[data stringValue]]; //[bytesRead setIntValue:[bytesRead intValue]+len]; }else{ NSLog(@'No Buffer'); } break ; }
declare your buffer as:
and do the append as: