편집

다음을 통해 공유


NetworkReachability Class

Definition

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

public class NetworkReachability : IDisposable, ObjCRuntime.INativeObject
type NetworkReachability = class
    interface INativeObject
    interface IDisposable
Inheritance
NetworkReachability
Implements

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

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

Handle

Handle (pointer) to the unmanaged object representation.

Methods

Dispose()

Releases the resources used by the NetworkReachability object.

Dispose(Boolean)

Releases the resources used by the NetworkReachability object.

Finalize()

Finalizer for the NetworkReachability object

GetFlags(NetworkReachabilityFlags)

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

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 given run loop.

Unschedule(CFRunLoop, String)

Removes the NetworkRechability from the given run loop.

Applies to

See also