views:

1048

answers:

2

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 ;
 }
+4  A: 

declare your buffer as:

uint8_t buffer[1024];

and do the append as:

[data appendBytes:buffer length:len];
Christopher Lloyd
Dutchie432
OIC! I got it. Thanks very much. Now just have to figure out how to convert the byte array to NSString. Thanks much!!
Dutchie432
Look into [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] and release it when you are done.
Chris Lundie
Boot,Can you expand on that a little. I have tried this:NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]but 'string' yields a nil value.
Dutchie432
+8  A: 

This is a problem down at the C level: you're confused about buffers and pointers.

This code:

uint8_t *buffer[1024];

gives you a stack buffer of 1024 pointers to uint8_ts, which is almost certainly not what you want. Instead:

uint8_t buffer[1024];

Later on, you're passing the address of your pointer on the stack to -[NSMutableData appendBytes:length:], which again is not what you want: as in the documentation, pass the first element:

[data appendBytes:buffer length:len];

There's a very thorough programming guide with complete code for what you're trying to do, you may want to reference it.

As for __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__, memory corruption and bad pointer dereferencing isn't something you can catch with an Objective-C @try/@catch; it's much lower-level. On the other hand, you can still catch this in the debugger if you turn debugging on.

Jim Puls
Thank you very much for your detailed explanation. Being a PC (vb and .net) programmer, you could imagine how this could be overwhelming to a 3-day XCode programmer. :) Its very refresging to get a detailed, non-snobby answer. :) When i get the rep I will vote up your answers.
Dutchie432
+1 for elaborating on the solution with such detail
Jarret Hardie