Share via


NetworkReachability Class

Definition

Used to detect the reachability of the network and to get notifications on network reachability changes.

public class NetworkReachability : CoreFoundation.NativeObject
type NetworkReachability = class
    inherit NativeObject
Inheritance
NetworkReachability

Remarks

You instantiate this class with a hostname or an IP address, and then use the TryGetFlags(NetworkReachabilityFlags) to get the reachability status.

To be notified of changes in the reachability of the specified host, you use the SetNotification(NetworkReachability+Notification) method to register a callback that will be invoked when there is a network reachability event and then call one of the Schedule(CFRunLoop, String) methods to start the event delivery.   Additionally,  SetDispatchQueue(DispatchQueue) can be used to configure the queue upon which the notification is invoked.

You can detect the ad-hoc WiFi network using the IP address 169.254.0.0 and the general network availability with 0.0.0.0.

// Track the reachability to a specific host
void Setup ()
{
    reachability = new NetworkReachability ("www.google.com");

    // Need to probe before we queue, or we wont get any meaningful values
    // this only happens when you create NetworkReachability from a hostname
    reachable = reachability.TryGetFlags (out flags);

    reachability.SetNotification (OnChange);
    reachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
}

void OnChange (NetworkReachabilityFlags flags)
{
    Console.WriteLine ("New state for host: " + flags);
}

Constructors

Name Description
NetworkReachability(IPAddress, IPAddress)

Creates a network reachability object from a local IP address and a remote one.

NetworkReachability(IPAddress)

Creates a network reachability class based on an IP address.

NetworkReachability(String)

Creates a network reachability object from a hostname.

Properties

Name Description
Handle (Inherited from DisposableObject)
Owns (Inherited from DisposableObject)

Methods

Name Description
ClearHandle() (Inherited from DisposableObject)
Dispose() (Inherited from DisposableObject)
Dispose(Boolean)
Dispose(Boolean) (Inherited from NativeObject)
Equals(Object) (Inherited from DisposableObject)
GetCheckedHandle() (Inherited from DisposableObject)
GetFlags(NetworkReachabilityFlags)

Method used to get the current reachability flags for this host.

GetHashCode() (Inherited from DisposableObject)
InitializeHandle(NativeHandle) (Inherited from DisposableObject)
Release() (Inherited from NativeObject)
Retain() (Inherited from NativeObject)
Schedule()

Schedules the delivery of the events (what is set with SetCallback) on the current loop.

Schedule(CFRunLoop, String)

Schedules the delivery of the events (what is set with SetCallback) on the given run loop.

SetDispatchQueue(DispatchQueue)

Specifies the DispatchQueue to be used for callbacks.

SetNotification(NetworkReachability+Notification)

Configures the method to be invoked when network reachability changes.

TryGetFlags(NetworkReachabilityFlags)

Method used to get the current reachability flags for this host.

Unschedule()

Removes the NetworkRechability from the current run loop.

Unschedule(CFRunLoop, String)

Removes the NetworkRechability from the given run loop.

Extension Methods

Name Description
GetHandle(INativeObject)
GetNonNullHandle(INativeObject, String)

Applies to

See also