iphone - 使用 NSInputStream/NSOutputStream 通过 TCP 进行通信(iPhone)

标签 iphone networking tcp nsstream nsinputstream

是否可以在 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

Using NSStreams For A TCP Connection Without NSHost

关于iphone - 使用 NSInputStream/NSOutputStream 通过 TCP 进行通信(iPhone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674526/

相关文章:

c# - 异步 TCP 服务器/客户端不可靠数据包?

http - 在什么情况下我的浏览器会尝试为多个请求重新使用 TCP 连接?

java - 如何保护用户不应读取的客户端数据

c - 从以太网帧中提取 UDP 数据包有效负载

iphone - 在 iOS 的 objective-c 中创建模式窗口

用于拖放 img 的 JavaScript 不适用于 Ipad/Iphone

docker - 无法使用IP访问Docker数据库(仅本地主机)

C# TCP 套接字编程 - 确定连接是否已断开

iphone - 如何动态设置 UIlabel TextColor

iphone - 如何在运行时在 objective-c 中更改 uibutton 标题?