iOS:如何检查流连接是否超时

时间:2012-07-19 08:44:35

标签: ios stream timeout connection

在我的应用程序中,我使用

连接到服务器
- (void)connectToServerUsingCFStream:(NSString *) urlStr portNo: (uint) portNo

此功能由另一种方法

调用
- (void)connectToServer:(NSString *)serverName onPort:(int)portNo 
{

    [self connectToServerUsingCFStream:serverName portNo:portNo];

    while (!((iStream.streamStatus == 2) || (oStream.streamStatus == 2))) {

        continue; 
    }

    NSLog(@"Streams connected");

    [self sendLoginRequest];
}

现在我想知道是否有可能检查我的连接请求是否超时(可能有一定的时间值?)。有没有办法在我的while循环中处理这个问题,还是应该使用不同的东西?

提前致谢, Bautzi

1 个答案:

答案 0 :(得分:1)

我不知道你是如何完全实现连接的,但是我在XMPPFramework有一些连接代码,代码注释:

/**
 * XMPPReconnect handles automatically reconnecting to the xmpp server due to accidental disconnections.
 * That is, a disconnection that is not the result of calling disconnect on the xmpp stream.
 * 
 * Accidental disconnections may happen for a variety of reasons.
 * The most common are general connectivity issues such as disconnection from a WiFi access point.
 * 
 * However, there are several of issues that occasionaly occur.
 * There are some routers on the market that disconnect TCP streams after a period of inactivity.
 * In addition to this, there have been iPhone revisions where the OS networking stack would pull the same crap.
 * These issue have been largely overcome due to the keepalive implementation in XMPPStream.
 * 
 * Regardless of how the disconnect happens, the XMPPReconnect class can help to automatically re-establish
 * the xmpp stream so as to have minimum impact on the user (and hopefully they don't even notice).
 * 
 * Once a stream has been opened and authenticated, this class will detect any accidental disconnections.
 * If one occurs, an attempt will be made to automatically reconnect after a short delay.
 * This delay is configurable via the reconnectDelay property.
 * At the same time the class will begin monitoring the network for reachability changes.
 * When the reachability of the xmpp host has changed, a reconnect may be tried again.
 * In addition to all this, a timer may optionally be used to attempt a reconnect periodically.
 * The timer is started if the initial reconnect fails.
 * This reconnect timer is fully configurable (may be enabled/disabled, and it's timeout may be changed).
 * 
 * In all cases, prior to attempting a reconnect,
 * this class will invoke the shouldAttemptAutoReconnect delegate method.
 * The delegate may use this opportunity to optionally decline the auto reconnect attempt.
 * 
 * Auto reconnect may be disabled at any time via the autoReconnect property.
 * 
 * Note that auto reconnect will only occur for a stream that has been opened and authenticated.
 * So it will do nothing, for example, if there is no internet connectivity when your application
 * first launches, and the xmpp stream is unable to connect to the host.
 * In cases such as this it may be desireable to start monitoring the network for reachability changes.
 * This way when internet connectivity is restored, one can immediately connect the xmpp stream.
 * This is possible via the manualStart method,
 * which will trigger the class into action just as if an accidental disconnect occurred.
**/

我不知道这个XMPPReconect class是否符合您的要求。