在 IOS Socket 程序方面我需要一些帮助。 我已经在 IOS 和一个 Java 服务器之间成功实施了 TCP Socket 连接, 但我发现在插座失效后无法重新连接( 我切断了网络并重新连接了网络 ) 。
我用来打开溪流的代码是:
- (void)initCommunication{
@try {
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"XXX.XXX.XXX.XXX", 4454, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
//Asignamos los delegates
inputStream.delegate = self;
outputStream.delegate = self;
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
connected = true;
}
}
@catch (NSException *exception) {
connected = false;
NSLog(@"Error connecting socket: %@", exception.reason);
[self checkConnection];
}
}
当我收到NSStreamEventEventError Ocrcrod 或NSSStreamEventEnd对质时, 我试图关闭连接并重新连接。
我关闭它与:
-(void) close{
[inputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream close];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
inputStream = nil;
[outputStream setDelegate:nil];
outputStream = nil;
connected = false;
}
问题在于套座不再连接... 我正在考虑使用小套子或可可套子来查看我能否重新连接套座... 您看到任何执行问题吗? 您建议使用小套子或可可套子吗?
提前感谢!