C# Ping
最后修改于 2023 年 7 月 5 日
在本文中,我们将展示如何使用 C# 中的 Ping 类确定远程主机的可访问性。
Ping
位于 System.Net.NetworkInformation
命名空间中。
Ping
类的名称和功能来自经典的 ping 网络实用程序。 ping 实用程序用于测试 IP 网络上远程主机的可达性。它基于 ICMP 协议运行。
Internet 控制消息协议 (ICMP) 供主机和路由器用于相互通信网络层信息。 通常,它用于错误报告。该协议由 RFC 792 描述。
Ping 是向目标主机发送 ICMP 回显请求并等待 ICMP 回显回复。
Ping
的 Send
和 SendAsync
方法向远程计算机发送 ICMP 回显请求消息,并等待来自该计算机的 ICMP 回显回复消息。 PingReply
提供有关消息状态和数据的信息。
C# Ping 发送
Send
方法以同步方式将 ICMP 回显请求消息发送到远程计算机。
using System.Net.NetworkInformation; string url = "example.com"; using var ping = new Ping(); PingReply res = ping.Send(url); Console.WriteLine(res.Status);
该程序以同步方式 ping 远程主机。
using System.Net.NetworkInformation;
Ping
位于 System.Net.NetworkInformation
命名空间中。
using var ping = new Ping();
创建一个 Ping
对象。
PingReply res = ping.Send(url);
我们 ping 由 URL 指定的主机并接收 PingReply
。
Console.WriteLine(res.Status);
我们打印回复的状态代码。
$ dotnet run Success
C# PingReply.RoundtripTime
PingReply.RoundtripTime
返回发送 ICMP 回显请求并接收相应 ICMP 回显回复消息所花费的毫秒数。
using System.Net.NetworkInformation; string url = "example.com"; using var ping = new Ping(); for (int i = 0; i < 3; i++) { PingReply res = await ping.SendPingAsync(url); Console.WriteLine($"{res.Status} from {res.Address} in {res.RoundtripTime} ms"); }
该程序发送三个异步 ping 请求,并返回状态、地址和往返时间。
Console.WriteLine($"{res.Status} from {res.Address} in {res.RoundtripTime} ms");
往返时间通过 PingReply
的 RoundtripTime
属性检索。
$ dotnet run Success from 2606:2800:220:1:248:1893:25c8:1946 in 199 ms Success from 2606:2800:220:1:248:1893:25c8:1946 in 129 ms Success from 2606:2800:220:1:248:1893:25c8:1946 in 145 ms
Ping 超时
一些重载方法允许为 ping 指定超时选项。
$ sudo tc qdisc add dev lo root netem delay 800ms
我们使用 Linux 流量控制实用程序为 localhost 添加 800 毫秒的延迟。
$ sudo tc qdisc del dev lo root
此命令删除 lo 接口的所有规则。
using System.Net.NetworkInformation; string url = "localhost"; using var ping = new Ping(); int timeout = 500; PingReply res = await ping.SendPingAsync(url, timeout); Console.WriteLine($"{res.Status} from {res.Address} in {res.RoundtripTime} ms");
在该程序中,我们指定 500 毫秒的超时。 我们 ping localhost。
$ dotnet run TimedOut from 0.0.0.0 in 0 ms
在使用流量控制实用程序添加超时规则后,我们得到了超时状态。
$ dotnet run Success from 127.0.0.1 in 0 ms
删除延迟规则后,我们收到成功状态。
C# ping 示例
在下一个示例中,我们将创建一个程序,该程序接受数据包数量和超时的选项。
using System.Net.NetworkInformation; string? url = string.Empty; int c = 3; int w = 0; int n = args.Length; if (n == 1) { url = args[0]; } else if (n == 2) { url = args[0]; c = int.Parse(args[1]); } else if (n == 3) { url = args[0]; c = int.Parse(args[1]); w = int.Parse(args[2]); } else { Console.WriteLine("wrong number of parameters"); return; } using var ping = new Ping(); if (w > 0) { for (int i = 0; i < c; i++) { PingReply res = await ping.SendPingAsync(url, w); Console.WriteLine($"{res.Status} from {res.Address} in {res.RoundtripTime} ms"); } } else { for (int i = 0; i < c; i++) { PingReply res = await ping.SendPingAsync(url); Console.WriteLine($"{res.Status} from {res.Address} in {res.RoundtripTime} ms"); } }
第一个参数是强制性的,是 URL。 下两个可选参数是数据包的数量和超时。
$ dotnet run example.com 2 Success from 2606:2800:220:1:248:1893:25c8:1946 in 128 ms Success from 2606:2800:220:1:248:1893:25c8:1946 in 125 ms
来源
在本文中,我们使用了 C# Ping 类来 ping 远程主机。
作者
列出所有 C# 教程。