2

I am using tcp connection to connect a server using ip and port. I can write and read stream on it. my problem is when I turn off server .app stop with "EXC_BAD_ACCESS" can anyone help me?

this is connect code:

-(void) connectToServerUsingStream:(NSString *)urlStr 
 portNo: (uint) portNo {
 if (![urlStr isEqualToString:@""]) 
 {
 NSURL *website = [NSURL URLWithString:urlStr];
 if (!website) 
 {
 NSLog(@"%@ is not a valid URL");
 return;
 }
 else
 {
 [NSStream getStreamsToHostNamed:urlStr 
 port:portNo 
 inputStream:&iStream
 outputStream:&oStream]; 
 [iStream retain];
 [oStream retain];
 [iStream setDelegate:self];
 [oStream setDelegate:self];
 [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
 forMode:NSDefaultRunLoopMode];
 [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
 forMode:NSDefaultRunLoopMode];
 [oStream open];
 [iStream open]; 
 }
 } 
}

and this is stream event delegate:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
 switch(eventCode) {
 case NSStreamEventHasBytesAvailable:
 {
 if (data == nil) 
 {
 data = [[NSMutableData alloc] init];
 }
 uint8_t buf[1024];
 unsigned int len = 0;
 len = [(NSInputStream *)stream read:buf maxLength:1024];
 if(len) 
 { 
 [data appendBytes:(const void *)buf length:len];
 int bytesRead;
 bytesRead += len;
 }
 else 
 {
 NSLog(@"No data.");
 return;
 }
 NSString *str = [[NSString alloc] initWithData:data 
 encoding:NSUTF8StringEncoding];
 NSLog(@"From server: %@",str);
 [str release];
 [data release]; 
 data = nil;
 break;
 } 
 case NSStreamEventErrorOccurred:
 {
 NSError *theError = [stream streamError];
 NSLog(@"Error reading stream! ,Error %i: %@",[theError code], [theError localizedDescription]);
 [self disconnect];
 [self connectToServerUsingStream:kHostIP portNo:kPort];
 break;
 }
 case NSStreamEventHasSpaceAvailable:
 {
 if(stream == oStream && !isDataSent)
 {
 isDataSent = YES;
 [self writeToServer:@"HI"];
 }
 break;
 }
 }
}
asked Jun 23, 2011 at 9:21

3 Answers 3

1

You should probably implement NSStreamEventEndEncountered case and close / release the stream.

answered Aug 9, 2011 at 0:10
Sign up to request clarification or add additional context in comments.

Comments

0

What logging messages are you getting so you can see which parts of your code get called?

Have you tried setting some breakpoints and then stepping through to see the exact point of failure and also then inspect the objects in case you aren't retaining/releasing properly?

What other debugging steps have you taken so far?

answered Jun 23, 2011 at 9:56

1 Comment

thanks all for answers the problem was that the length of data from server after switching off is -1 and this doesn't handle in NSStreamEventHasBytesAvailable case
0

Try adding the following linker flags:

OTHER_LDFLAGS = -lz -lxml2 -ltidy -ObjC

To your project.

answered Jul 13, 2012 at 9:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.