NetworkMonitor


Object Hierarchy:

GLib.NetworkMonitor GLib.NetworkMonitor GLib.NetworkMonitor GLib.Object GLib.Object GLib.Object->GLib.NetworkMonitor GLib.Initable GLib.Initable GLib.Initable->GLib.NetworkMonitor

Description:

[ CCode ( type_cname = "GNetworkMonitorInterface" , type_id = "g_network_monitor_get_type ()" ) ]
[ Version ( since = "2.32" ) ]
public interface NetworkMonitor : Object, Initable

NetworkMonitor provides an easy-to-use cross-platform API for monitoring network connectivity.

On Linux, the available implementations are based on the kernel's netlink interface and on NetworkManager.

There is also an implementation for use inside Flatpak sandboxes.

Example: Network Monitoring:

public static int main (string[] args) {
NetworkMonitor monitor = NetworkMonitor.get_default ();

//
// Check whether the network is considered available:
// (network availability != internet)
//

bool available = monitor.get_network_available ();
print ("Network available: %s\n", available.to_string ());


//
// Determine whether valadoc.org can be reached: (sync):
//

NetworkAddress address = new NetworkAddress ("www.valadoc.org", 80);
if (available == true) {
try {
bool can_reach = monitor.can_reach (address);
print ("can-reach: %s\n", can_reach.to_string ());
} catch (Error e) {
print ("Error: %s\n", e.message);
}
}


//
// Recheck availability when the network configuration changes:
//

monitor.network_changed.connect ((available) => {
print ("Network changed (available: %s)\n", available.to_string ());

// Determine whether valadoc.org can be reached: (async):
if (available == true) {
monitor.can_reach_async.begin (address, null, (obj, res) => {
try {
bool can_reach = monitor.can_reach_async.end (res);
print ("can-reach: %s\n", can_reach.to_string ());
} catch (Error e) {
print ("Error: %s\n", e.message);
}
});
}
});

new MainLoop ().run ();
return 0;
}

valac --pkg gio-2.0 GLib.NetworkMonitor.vala

Namespace: GLib
Package: gio-2.0

Content:

Properties:

Static methods:

Methods:

Signals:

Inherited Members:

All known members inherited from class GLib.Object
All known members inherited from interface GLib.Initable



2022 vala-language.org