我已经编写了一个程序,使用苹果流编程指南中概述的NSStream协议连接到给定IP上的服务器。连接和数据传输运作良好,但如果用户指定了错误的IP并且程序尝试打开流,则会导致程序无响应。
根据我所阅读的内容,handleEvent方法可以检测流错误,但是当我检查eventCode == NSStreamEventErrorOccurred条件时,似乎没有发生任何事情。我的连接代码如下:
NSString *hostString = ipField.text;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)hostString, 10001, &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];
有什么办法可以指定超时值或允许按钮触发关闭流,如果无法建立连接?