我编写了这段代码来设置与服务器的流:
-(void)streamOpenWithIp:(NSString *)ip withPortNumber:(int)portNumber;
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)ip, portNumber, &readStream, &writeStream);
if(readStream && writeStream)
{
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
//Setup inpustream
inputStream = (__bridge NSInputStream *)readStream;
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
//Setup outputstream
outputStream = (__bridge NSOutputStream *)writeStream;
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
}
}
我能够连接到服务器并发送和接收数据。但我想检查连接是否仍然存在。如果我从 wifi 路由器上断开电缆,我仍然可以在流中发送数据并且没有发生错误。
您可以在应用程序级别解决此问题,方法是使用计时器发送一些消息并检查您是否收到回复。但是您也可以在较低级别使用 TCP Keep-Alive 技术解决此问题。
我如何用 NSStream 实现这个?如何设置检查间隔?
我假设您在流关闭时通过 TCP Keep-Alive 检查得到 NSStreamEventErrorOcurred?
我已经检查了这篇文章,但我无法弄清楚: 在 iOS 中保持套接字连接处于活动状态
谢谢你的帮助!