如何在iOS中建立Peer to Peer连接?

时间:2013-11-27 11:00:40

标签: ios iphone objective-c voip

我想为iPhone实施一个Voip应用程序。

如何在使用套接字编程和流式传输的用户之间建立点对点连接。

任何可用的源代码。

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

定义类属性:

NSInputStream *inputStream;
NSOutputStream *outputStream;

初始化你的溪流

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)kScannerIP, kScannerPort, &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];

实现委托方法来处理传入的数据:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    switch (streamEvent) {
        case NSStreamEventOpenCompleted:
            break;

        case NSStreamEventHasBytesAvailable:
            break;

        case NSStreamEventHasSpaceAvailable:
            break;

        case NSStreamEventErrorOccurred:
            break;

        case NSStreamEventEndEncountered:
            break;

        default:
            NSLog(@"Unknown event");
    }
}

最后关闭你的网络:

[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
inputStream = nil;
outputStream = nil;

答案 2 :(得分:-2)

你不能;当你的设备的IP都发生变化时(两者都转移到不同的网络)会发生什么?