Eventi
Creare app e agenti di intelligenza artificiale
17 mar, 21 - 21 mar, 10
Partecipa alla serie meetup per creare soluzioni di intelligenza artificiale scalabili basate su casi d'uso reali con altri sviluppatori ed esperti.
Iscriviti subitoQuesto browser non è più supportato.
Esegui l'aggiornamento a Microsoft Edge per sfruttare i vantaggi di funzionalità più recenti, aggiornamenti della sicurezza e supporto tecnico.
Lo spazio dei nomi System.Net.NetworkInformation consente di raccogliere informazioni sugli eventi, le modifiche, le statistiche e le proprietà della rete. In questo articolo si apprenderà come usare la classe System.Net.NetworkInformation.NetworkChange per determinare se l'indirizzo di rete o la disponibilità sono cambiati. Inoltre, verranno visualizzate le statistiche e le proprietà della rete in base a un'interfaccia o a un protocollo. Infine, verrà usata la classe System.Net.NetworkInformation.Ping per determinare se un host remoto è raggiungibile.
La classe System.Net.NetworkInformation.NetworkChange consente di determinare se l'indirizzo di rete o la disponibilità sono cambiati. Per usare questa classe, creare un gestore eventi per elaborare la modifica e associarlo a un NetworkAddressChangedEventHandler o NetworkAvailabilityChangedEventHandler.
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
static void OnNetworkAvailabilityChanged(
object? sender, NetworkAvailabilityEventArgs networkAvailability) =>
Console.WriteLine($"Network is available: {networkAvailability.IsAvailable}");
Console.WriteLine(
"Listening changes in network availability. Press any key to continue.");
Console.ReadLine();
NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged;
Il codice C# precedente:
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
static void OnNetworkAddressChanged(
object? sender, EventArgs args)
{
foreach ((string name, OperationalStatus status) in
NetworkInterface.GetAllNetworkInterfaces()
.Select(networkInterface =>
(networkInterface.Name, networkInterface.OperationalStatus)))
{
Console.WriteLine(
$"{name} is {status}");
}
}
Console.WriteLine(
"Listening for address changes. Press any key to continue.");
Console.ReadLine();
NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
Il codice C# precedente:
È possibile raccogliere le statistiche e le proprietà della rete in base a un'interfaccia o a un protocollo. Le classi NetworkInterface, NetworkInterfaceType e PhysicalAddress forniscono informazioni su una particolare interfaccia di rete, mentre le classi IPInterfaceProperties, IPGlobalProperties, IPGlobalStatistics, TcpStatistics e UdpStatistics offrono informazioni sui pacchetti del livello 3 e 4.
ShowStatistics(NetworkInterfaceComponent.IPv4);
ShowStatistics(NetworkInterfaceComponent.IPv6);
static void ShowStatistics(NetworkInterfaceComponent version)
{
var properties = IPGlobalProperties.GetIPGlobalProperties();
var stats = version switch
{
NetworkInterfaceComponent.IPv4 => properties.GetTcpIPv4Statistics(),
_ => properties.GetTcpIPv6Statistics()
};
Console.WriteLine($"TCP/{version} Statistics");
Console.WriteLine($" Minimum Transmission Timeout : {stats.MinimumTransmissionTimeout:#,#}");
Console.WriteLine($" Maximum Transmission Timeout : {stats.MaximumTransmissionTimeout:#,#}");
Console.WriteLine(" Connection Data");
Console.WriteLine($" Current : {stats.CurrentConnections:#,#}");
Console.WriteLine($" Cumulative : {stats.CumulativeConnections:#,#}");
Console.WriteLine($" Initiated : {stats.ConnectionsInitiated:#,#}");
Console.WriteLine($" Accepted : {stats.ConnectionsAccepted:#,#}");
Console.WriteLine($" Failed Attempts : {stats.FailedConnectionAttempts:#,#}");
Console.WriteLine($" Reset : {stats.ResetConnections:#,#}");
Console.WriteLine(" Segment Data");
Console.WriteLine($" Received : {stats.SegmentsReceived:#,#}");
Console.WriteLine($" Sent : {stats.SegmentsSent:#,#}");
Console.WriteLine($" Retransmitted : {stats.SegmentsResent:#,#}");
Console.WriteLine();
}
Il codice C# precedente:
ShowStatistics
per visualizzare le statistiche per ogni protocollo.ShowStatistics
chiama IPGlobalProperties.GetIPGlobalProperties() e, a seconda dell'oggetto specificato NetworkInterfaceComponent, chiamerà IPGlobalProperties.GetIPv4GlobalStatistics() o IPGlobalProperties.GetIPv6GlobalStatistics().È possibile usare la classe Ping per determinare se un host remoto è attivo, in rete e raggiungibile.
using Ping ping = new();
string hostName = "stackoverflow.com";
PingReply reply = await ping.SendPingAsync(hostName);
Console.WriteLine($"Ping status for ({hostName}): {reply.Status}");
if (reply is { Status: IPStatus.Success })
{
Console.WriteLine($"Address: {reply.Address}");
Console.WriteLine($"Roundtrip time: {reply.RoundtripTime}");
Console.WriteLine($"Time to live: {reply.Options?.Ttl}");
Console.WriteLine();
}
Il codice C# precedente:
"stackoverflow.com"
.Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback:
Eventi
Creare app e agenti di intelligenza artificiale
17 mar, 21 - 21 mar, 10
Partecipa alla serie meetup per creare soluzioni di intelligenza artificiale scalabili basate su casi d'uso reali con altri sviluppatori ed esperti.
Iscriviti subitoFormazione
Percorso di apprendimento
Configurare la rete nei client Windows - Training
MD-100 Configurare la rete nei client Windows
Certificazione
Microsoft Certified: Azure Network Engineer Associate - Certifications
Illustrare la progettazione, l'implementazione e la manutenzione dell'infrastruttura di rete di Azure, il bilanciamento del carico del traffico, il routing di rete e altro ancora.