【问题标题】:How to detect network disconnect?如何检测网络断开?
【发布时间】:2012-04-30 23:19:03
【问题描述】:

我正在开发适用于 Mac OS X 10.7/10.6 的网络应用程序。我需要检测网络问题。没有互联网连接时我需要检测的另一个词。我尝试使用连接到服务器的计时器添加一些方法,如果没有结果则显示消息。但我需要更多的实时解决方案。 是否有一些关于网络断开的系统通知?

【问题讨论】:

    标签: objective-c cocoa notifications


    【解决方案1】:

    如果你使用 nsstreams,那么做

    标题:

    @interface CrestronClient : UIViewController <NSStreamDelegate,UIAlertViewDelegate> {
    
        NSInputStream *inputStream;
        NSOutputStream *outputStream;
    
    }
    @end
    

    .m 文件:

    初始化值通常在连接方法中或确实加载

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, 192.168.1.1, 46651, &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];
    
        unsigned char connectByteArray1[] = {
            0x01, 0x00, 0x07, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x03, 0x40
        };
    
        [outputStream write:connectByteArray1 maxLength:sizeof(connectByteArray1)];
    

    然后你的委托方法:

    - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    
        switch (streamEvent) 
        {
            case NSStreamEventOpenCompleted:
            {
                DDLogVerbose(@"Stream opened");
                break;
            }
    
            case NSStreamEventHasBytesAvailable:
            {
                if(!rawData) {
                    rawData = [[NSMutableData data] retain];
                }
                uint8_t buf[1024];
                unsigned int len = 0;
                len = [(NSInputStream *)theStream read:buf maxLength:1024];
                if(len) {
                    [rawData initWithBytes:buf length:len];                
                } else {
                    DDLogVerbose(@"no buffer!");
                }
            }   
            case NSStreamEventErrorOccurred:
            {
                if ([theStream isKindOfClass:[NSInputStream class]]) {
                    NSString* address = [self getAddress];
                    NSString* myIPAdress = [NSString stringWithFormat:@"IP Address: %@", address];
    
                    //[cClient updateRequest];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cant Connect" message:[NSString stringWithFormat:@"Cant connect to server: %@, Make sure you are connected to the proper wireless network.  Your Ip Address is %@",CCV.ipAddress,myIPAdress] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Reconnect", nil];
    
                    [alert show];
                    [alert release];            
                }
                break;   
            }
    
            case NSStreamEventEndEncountered:
            {
                [theStream close];
                [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                [theStream release];
                break;
            }
            case NSStreamEventHasSpaceAvailable:
            {
                //DDLogVerbose(@"has space available");
                break;
            }
    
            case NSStreamEventNone:
            {
                DDLogVerbose(@"none");
                break;
            }
    
            default:
            {
                DDLogVerbose(@"Unknown event");
            }
        }
    }
    

    可以看到有已连接、未连接、已断开的情况 这就是你需要的

    【讨论】:

    • 谢谢!如果我们连接到某个主机,这是一个很好的解决方案。但是当我们启动应用程序并没有连接到主机,然后拔掉网线或关闭WiFi连接怎么办?
    • 如果我在错误的 wifi 网络上(意味着我无法访问我的目标 ip),NSStreamEventErrorOccurred 会触发它,因为它无法连接,这就是为什么我在那里发出警报以“重新连接”或重试连接
    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2013-01-24
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多