我是 Objective C 和 ios 开发的新手。我通常使用 Java 编写 Android 应用程序,并且总是使用 onPause 调用来关闭我的套接字。但是,当我在 applicationWillResignActive 下调用我的关闭套接字方法时,我与我的流侦听器失去了通信,并且无法关闭输入和输出流。事实上,即使应用程序连接到服务器,两个流的流状态都显示 NSStreamStatusNotOpen 或 0。有任何想法吗?
谢谢
编辑
SocketHandler.m 类中的连接方法:
-(void)connect:(int *)port ipAddress:(NSString *)ipAddress
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ipAddress, port, &readStream, &writeStream);
inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
NSLog(@"input stream id %i", inputStream);
}
-(void)disconnect{
NSLog(@"disconnect method called");
socketStatus = [outputStream streamStatus];
int status = socketStatus;
NSLog(@"Stream Status is %i", status);
if (status == 2) {
[inputStream close];
[outputStream close];
NSLog(@"Socket Closed");
}
}