NetworkMonitor
Object Hierarchy:
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
Content:
Properties:
Static methods:
Methods:
Signals:
Inherited Members:
All known members inherited from class GLib.Object
- @get
- @new
- @ref
- @set
- add_toggle_ref
- add_weak_pointer
- bind_property
- connect
- constructed
- disconnect
- dispose
- dup_data
- dup_qdata
- force_floating
- freeze_notify
- get_class
- get_data
- get_property
- get_qdata
- get_type
- getv
- interface_find_property
- interface_install_property
- interface_list_properties
- is_floating
- new_valist
- new_with_properties
- newv
- notify
- notify_property
- ref_count
- ref_sink
- remove_toggle_ref
- remove_weak_pointer
- replace_data
- replace_qdata
- set_data
- set_data_full
- set_property
- set_qdata
- set_qdata_full
- set_valist
- setv
- steal_data
- steal_qdata
- thaw_notify
- unref
- watch_closure
- weak_ref
- weak_unref
All known members inherited from interface GLib.Initable