9

我有一个 VoIP 应用程序,它使用 TCP 服务在来电时唤醒它。TCP 套接字是使用以下代码片段创建的:

CFReadStreamRef read = NULL;
CFWriteStreamRef write = NULL;
...
CFStreamCreatePairWithSocketToHost(NULL,(__bridge CFStringRef)shost, port, &read, &write);
self.read = (__bridge NSInputStream*)read;
self.write = (__bridge NSOutputStream*)write;
if (![self.read setProperty:NSStreamNetworkServiceTypeVoIP
                     forKey:NSStreamNetworkServiceType]){
    [Log log:@"Could not set VoIP mode to read stream"];
}
if (![self.write setProperty:NSStreamNetworkServiceTypeVoIP
                      forKey:NSStreamNetworkServiceType]){
    [Log log:@"Could not set VoIP mode to write stream"];
}
self.read.delegate = self;
self.write.delegate = self;
CFRelease(read);
CFRelease(write);
[self.read scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[self.write scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[self.read open];
[self.write open];

我还设置了以下内容:

  1. 信息列表中的 VoIP 和音频
  2. 使用 [UIApplication sharedApplication] setKeepAliveTimeout 保持活动计时器
  3. UIRequiresPersistentWiFi = YES 在 info plist 中(很确定它不是必需的,但是......)

这在应用程序处于前台时运行良好,甚至在后台运行几分钟,但几分钟后 - 应用程序没有收到任何新的 TCP 消息。它不适用于 wifi 或 3G,两者的结果相同。

我还尝试将属性设置为读取流(尽管读取和写入指向同一个套接字)。每当我在 TCP 上接收数据或发送数据时,我也会启动一个简短的后台任务。顺便说一句 - 一切都发生在主线程上。
我检查了应用程序是否崩溃 - 它没有。
在设备上调试时可以观察到相同的行为 - 一段时间后 - 没有收到任何东西(没有崩溃、警告等)。

我究竟做错了什么?

4

3 回答 3

3

尝试以下代码。确保您的应用中只有一个 voip 套接字。

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"1.2.3.4",9999, &readStream, &writeStream);

CFReadStreamSetProperty(readStream,kCFStreamNetworkServiceType,kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty(writeStream, kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);

inputStream = (NSInputStream *)readStream;
[inputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];

outputStream = (NSOutputStream *)writeStream;
[outputStream setDelegate:self];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream open];
于 2012-08-30T15:47:30.620 回答
3

看起来你的代码应该可以工作。但我能想到的可能有两个技术问题:

  1. 如果您从 LAN 连接尝试此操作,而应用程序在后台 LAN 路由器可以关闭被动 TCP 连接,因为在这种情况下,SIP 堆栈(猜你使用 SIP 协议)无法像它那样每 15 到 30 秒发送数据保持活动状态在前台。

  2. 不太可能,假设您知道自己在做什么,但是由于注册保持活动在后台只能在 10 分钟内触发一次,因此请确保 SIP 服务器允许如此长的过期时间,并且您在注册消息中正确定义它。

于 2012-11-01T10:34:26.783 回答
0

ViewController.h文件中添加

@property (nonatomic, strong) NSInputStream *inputStream;
@property (nonatomic, strong) NSOutputStream *outputStream;
@property (nonatomic) BOOL sentPing;

ViewController.m文件中添加之后@implementation ViewController

const uint8_t pingString[] = "ping\n";
const uint8_t pongString[] = "pong\n";

在里面添加以下代码viewDidLoad

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(@"192.168.0.104"), 10000, &readStream, &writeStream);

//in above line user your MAC IP instead of 192.168.0.104

self.sentPing = NO;
//self.communicationLog = [[NSMutableString alloc] init];
self.inputStream = (__bridge_transfer NSInputStream *)readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[self.inputStream setProperty:NSStreamNetworkServiceTypeVoIP forKey:NSStreamNetworkServiceType];
[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];

//After every 10 mins this block will be execute to ping server, so connection will be live for more 10 mins
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
    if (self.outputStream)
    {
        [self.outputStream write:pingString maxLength:strlen((char*)pingString)];
        //[self addEvent:@"Ping sent"];
    }
}];


- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
    case NSStreamEventNone:
        // do nothing.
        break;

    case NSStreamEventEndEncountered:
        //[self addEvent:@"Connection Closed"];
        break;

    case NSStreamEventErrorOccurred:
        //[self addEvent:[NSString stringWithFormat:@"Had error: %@", aStream.streamError]];
        break;

    case NSStreamEventHasBytesAvailable:
        if (aStream == self.inputStream)
        {
            uint8_t buffer[1024];
            NSInteger bytesRead = [self.inputStream read:buffer maxLength:1024];
            NSString *stringRead = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
            stringRead = [stringRead stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

            //[self addEvent:[NSString stringWithFormat:@"Received: %@", stringRead]];


            //if server response is 'call' then a notification will go to notification center and it will be fired
            //immediately and it will popup if app is in background.
            if ([stringRead isEqualToString:@"call"])
            {
                UILocalNotification *notification = [[UILocalNotification alloc] init];
                notification.alertBody = @"New VOIP call";
                notification.alertAction = @"Answer";
                //[self addEvent:@"Notification sent"];
                [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
            }
            //else if ([stringRead isEqualToString:@"ping"])
            //{
            //if server response is 'ping' then a sting 'pong' will go to server immediately
            //[self.outputStream write:pongString maxLength:strlen((char*)pongString)];
            //}
        }
        break;

    case NSStreamEventHasSpaceAvailable:
        if (aStream == self.outputStream && !self.sentPing)
        {
            self.sentPing = YES;
            if (aStream == self.outputStream)
            {
                [self.outputStream write:pingString maxLength:strlen((char*)pingString)];
                //[self addEvent:@"Ping sent"];
            }
        }
        break;

    case NSStreamEventOpenCompleted:
        if (aStream == self.inputStream)
        {
            //[self addEvent:@"Connection Opened"];
        }
        break;

    default:
        break;
}
}

构建您的应用程序并运行

在您的 MAC PC 中打开终端并写入nc -l 10000并按 Enter

$ nc -l 10000

然后写call并按回车

于 2014-09-09T08:58:26.543 回答