防止在轮询模式下使用 NSStream 停止应用程序

发布于 2024-12-27 20:50:34 字数 770 浏览 0 评论 0原文

我已经实现了从 Windows 桌面应用程序传输到 iPhone 应用程序的客户端-服务器。我使用 NSStream 以轮询模式(同步)传输数据。

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)urlStr, portNo, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;

[inputStream open];
[outputStream open];

一切正常,但是当我尝试连接不存在的服务器或断开连接的服务器或端口号/IP 地址错误时,NSOutputStream 对象的 WRITE 方法会停止应用程序执行。

const uint8_t *str = (uint8_t *) [strRichiesta cStringUsingEncoding:NSASCIIStringEncoding];
[outputStream write:str maxLength:strlen((char*)str)]; 

是否可以通过插入超时控制来管理方法? 如果是的话我该怎么办?

我认为 NSInputStream 对象的 READ 方法也会出现同样的问题。

有人可以帮我吗?

I have implemented a client-server transferring from Windows desktop application to iPhone App. I transfer data using NSStream in polling mode (synchronous).

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)urlStr, portNo, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;

[inputStream open];
[outputStream open];

All works fine, but when I attempt to connect an inexistent server or a disconnected server or the port number/ip address is wrong, the WRITE method of NSOutputStream object, stops the application execution.

const uint8_t *str = (uint8_t *) [strRichiesta cStringUsingEncoding:NSASCIIStringEncoding];
[outputStream write:str maxLength:strlen((char*)str)]; 

Is it possible to manage the method by inserting timeout control?
If yes, how can I do?

I think that the same problem occurs also with READ method of NSInputStream object.

Could someone help me, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等数载,海棠开 2025-01-03 20:50:34

在尝试使用 NSStream 的 streamStatusstreamError 函数进行读写之前,请先检查它们。请参阅Apple的 NSStream 文档 了解更多信息。

这也可能有帮助: 设置套接字流

编辑:
NSStreamStatus 值:

typedef enum {
    NSStreamStatusNotOpen = 0,
    NSStreamStatusOpening = 1,
    NSStreamStatusOpen = 2,
    NSStreamStatusReading = 3,
    NSStreamStatusWriting = 4,
    NSStreamStatusAtEnd = 5,
    NSStreamStatusClosed = 6,
    NSStreamStatusError = 7
};

您必须等到状态为 Open 后才能使用它。错误的 URL 可能需要几秒钟(甚至可能是 30 秒)才能解析为错误状态。

Check the NSStreams' streamStatus and streamError functions before trying to use them for reading and writing. See Apple's NSStream Documentation for more info.

This also may help: Setting Up Socket Streams

EDIT:
NSStreamStatus values:

typedef enum {
    NSStreamStatusNotOpen = 0,
    NSStreamStatusOpening = 1,
    NSStreamStatusOpen = 2,
    NSStreamStatusReading = 3,
    NSStreamStatusWriting = 4,
    NSStreamStatusAtEnd = 5,
    NSStreamStatusClosed = 6,
    NSStreamStatusError = 7
};

You will have to wait until the status is Open before using it. It may several seconds (maybe even 30 sec) for a bad URL to resolve to status Error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文