dimanche 1 février 2015

Recovering a lost connection using sockets

I'm trying to dig deeper by using this example of an iPhone Chart Server and all is working as expected.


What I wanted to learn next is how to recover when the connection to the server is lost (for whatever reason).


First issue is that the app tries to open the input & output streams concurrently thus if I implement an alert, I get it two times.


The second is that if I'm another view controller and the connection is lost I can't seem to be able to recover it... I call the sendSocketMessage and if there is the error flat to try to use the initNetworkCommunication but I get a fatal error.



@interface ViewController ()

@end

@implementation ViewController

bool connectionError = false;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initNetworkCommunication];

messages = [[NSMutableArray alloc] init];

}


- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.1", 6035, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

}


- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

switch (streamEvent) {

case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;

case NSStreamEventHasBytesAvailable:

connectionError = false;
if (theStream == inputStream) {

uint8_t buffer[204800];
int len;

while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {

NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];

if (nil != output) {
//NSLog(@"server said: %@", output);
//NSLog(@"message received!");

[self messageReceived:output];
}
}
}
}
break;

case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
connectionError = true;
[self connectionLost];
break;

case NSStreamEventEndEncountered:
break;

default:
NSLog(@"Unknown event");
}

}

- (void) connectionLost {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Connection to the server lost!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[alert show];

}

- (void) messageReceived:(NSString *)message {

[messages addObject:message];
//NSLog(@"server said: %@", message);
//NSLog(@"message received!");

syncDevices *syncDev = [syncDevices new];
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];

NSArray *devArr;

devArr = [syncDev parseDeviceXMLData:data];

// extra parsing blah blah...

}

- (IBAction)joinChat:(id)sender {

[self initNetworkCommunication];

[self sendSocketMessage: @"iam:" message: _inputNameField.text];
}


- (void) sendSocketMessage:(NSString*) sendCommand message:(NSString*) sendMessage
{
// do something...

if (connectionError == true) {
[self initNetworkCommunication];
}

NSString *response = [NSString stringWithFormat: @"%@%@", sendCommand, sendMessage];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"clint sent: %@", response);
}



@end


The error I get is from here:



int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}


Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)


Any advice?




Aucun commentaire:

Enregistrer un commentaire