简体   繁体   中英

How to detect network disconnect?

I working on a network application for Mac OS X 10.7/10.6. And I need to detect network problem . Another words I need to detect when no internet connection. I tried to add some method with a timer which connect to server, and show message if no results. But I need more realtime solution. Is there some system notifications about network disconnect?

if you use nsstreams, then do

header:

@interface CrestronClient : UIViewController <NSStreamDelegate,UIAlertViewDelegate> {

    NSInputStream *inputStream;
    NSOutputStream *outputStream;

}
@end

.m File:

init values usually in a connect method or did load

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)];

then your delegate method:

- (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");
        }
    }
}

you can see that there are cases for connected, not connected, disconnected this is what you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM