是否可以在 iPhone 上使用 NSInputStream/NSOutputStream 进行 TCP 通信? Apple 在他们的文档中给出的示例使用 [NSStream getStreamsToHost] 并且在 iPhone 上不受支持。我看过其他使用 CFStream 设置套接字然后桥接到 NSStream 的帖子,这是唯一受支持的方式吗?
根据文档,理论上这样的事情似乎应该可行:
//input stream
NSURL *url = [[NSURL alloc] initWithString:@"10.252.1.1:8080"];
iStream = [NSInputStream inputStreamWithURL:url];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];
oStream = [NSOutputStream outputStreamWithURL:url append:true];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];
但是这有两个问题:
1) 如果我只执行 iStream 部分,我将看不到任何对我的委托(delegate)调用的事件。
2) outputStreamWithURL 失败并出现来自 CFWriteStreamSetProperty 的神秘“EXC_BAD_ACCESS”错误消息
最佳答案
这篇 Apple 文章解释了如何在 iOS 上实现 getStreamsStreamsToHost
关于iphone - 使用 NSInputStream/NSOutputStream 通过 TCP 进行通信(iPhone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674526/