我目前正在尝试连接到 tcp 服务器。一切都很好,但如果服务器不可用,应用程序会冻结大约 30 秒,这不是很好。我已经搜索了一个解决方案,但到目前为止找不到有用的东西。
也许你们中的某个人有一个检查连接的解决方案?
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)@"IP_SERVER_PATH", 6666, &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];