iPhone:使用 NSStream 捕获连接错误

发布于 2024-09-18 10:31:15 字数 920 浏览 9 评论 0原文

我编写了一个程序,使用 Apple 流编程指南中概述的 NSStream 协议连接到给定 IP 上的服务器。数据的连接和传输工作完美,但是如果用户指定了错误的 IP 并且程序尝试打开流,则会导致程序无响应。

根据我的阅读,handleEvent 方法检测到流错误,但是当我检查 eventCode == NSStreamEventErrorOccurred 的条件时,似乎什么也没有发生。我的连接代码如下:

NSString *hostString = ipField.text;

    CFReadStreamRef readStream;

    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)hostString, 10001, &readStream, &writeStream);



    inputStream = (NSInputStream *)readStream;

    outputStream = (NSOutputStream *)writeStream;

    [inputStream setDelegate:self];

    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];

    [outputStream open];

如果无法建立连接,是否知道如何指定超时值或允许按钮触发流的关闭?

I have written a program that connects to a server on a given IP using the NSStream protocol outlined in Apple's stream programming guide. The connection and transfer of data works flawlessly, however if the user specifies the wrong IP and the program attempts to open the streams it results in the program becoming unresponsive.

From what I have read, the handleEvent method detects stream errors, however when I check for the condition that eventCode == NSStreamEventErrorOccurred, nothing seems to happen. My connect code is as follows:

NSString *hostString = ipField.text;

    CFReadStreamRef readStream;

    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)hostString, 10001, &readStream, &writeStream);



    inputStream = (NSInputStream *)readStream;

    outputStream = (NSOutputStream *)writeStream;

    [inputStream setDelegate:self];

    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];

    [outputStream open];

Any idea as to how I can specify a timeout value or allow for a button to trigger the closing of the streams if a connection cannot be made?

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

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

发布评论

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

评论(1

日暮斜阳 2024-09-25 10:31:15

关于如何指定一个的任何想法
超时值或允许按钮
触发流的关闭,如果
无法建立连接?

使用NSTimer

在你的 .h 中:

...
@interface MyViewController : UIViewController
{
    ...
    NSTimer* connectionTimeoutTimer;
    ...
}
...

在你的 .m 中:

...
@interface MyViewController ()
@property (nonatomic, retain) NSTimer* connectionTimeoutTimer;
@end

@implementation MyViewController

...
@synthesize connectionTimeoutTimer;
...

- (void)dealloc
{
    [self stopConnectionTimeoutTimer];
    ...
}

// Call this when you initiate the connection
- (void)startConnectionTimeoutTimer
{
    [self stopConnectionTimeoutTimer]; // Or make sure any existing timer is stopped before this method is called

    NSTimeInterval interval = 3.0; // Measured in seconds, is a double

    self.connectionTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                                   target:self
                                                                 selector:@selector(handleConnectionTimeout:)
                                                                 userInfo:nil
                                                                  repeats:NO];
}

- (void)handleConnectionTimeout
{
    // ... disconnect ...
}

// Call this when you successfully connect
- (void)stopConnectionTimeoutTimer
{
    if (connectionTimeoutTimer)
    {
        [connectionTimeoutTimer invalidate];
        [connectionTimeoutTimer release];
        connectionTimeoutTimer = nil;
    }
}

Any idea as to how I can specify a
timeout value or allow for a button to
trigger the closing of the streams if
a connection cannot be made?

Use an NSTimer.

In your .h:

...
@interface MyViewController : UIViewController
{
    ...
    NSTimer* connectionTimeoutTimer;
    ...
}
...

In your .m:

...
@interface MyViewController ()
@property (nonatomic, retain) NSTimer* connectionTimeoutTimer;
@end

@implementation MyViewController

...
@synthesize connectionTimeoutTimer;
...

- (void)dealloc
{
    [self stopConnectionTimeoutTimer];
    ...
}

// Call this when you initiate the connection
- (void)startConnectionTimeoutTimer
{
    [self stopConnectionTimeoutTimer]; // Or make sure any existing timer is stopped before this method is called

    NSTimeInterval interval = 3.0; // Measured in seconds, is a double

    self.connectionTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:interval
                                                                   target:self
                                                                 selector:@selector(handleConnectionTimeout:)
                                                                 userInfo:nil
                                                                  repeats:NO];
}

- (void)handleConnectionTimeout
{
    // ... disconnect ...
}

// Call this when you successfully connect
- (void)stopConnectionTimeoutTimer
{
    if (connectionTimeoutTimer)
    {
        [connectionTimeoutTimer invalidate];
        [connectionTimeoutTimer release];
        connectionTimeoutTimer = nil;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文