【发布时间】:2012-07-08 09:49:24
【问题描述】:
我有以下用 Objective-C 编写的将数据写入套接字的代码。服务器在 Ubuntu 之上运行 node.js:
NSString *url = @"anIPAddress";
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)url, 9000, &readStream, &writeStream);
self.inputStream = (NSInputStream *)readStream;
self.outputStream = (NSOutputStream *)writeStream;
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream open];
[self.outputStream open];
我能够连接到服务器并发送信息。但是我注意到几分钟后连接超时(我认为是 5 分钟左右?)。我怎样才能保持这个连接活着?我知道存在断开连接,因为如果我在终端下连接到服务器,也会发生同样的事情,我必须保持该连接处于活动状态。我想我的代码中也发生了同样的事情。感谢您的帮助!
【问题讨论】:
-
对于有效的Objective-C代码,请参考following answer...
标签: objective-c sockets ios5 timeout keep-alive