//测试网络状态
Ping p = new Ping();
string pAddr = ConfigurationManager.AppSettings["pingAddr"];
//创建Ping对象p
PingReply pr;
pAddr = "www.baidu.com";
Console.WriteLine("Ping:" +pAddr );
int lost = 0; //丢包数
long rtt = 0; //ping回复时间
for (int i = 0; i < 4; i++)
{
//向指定IP或者主机名的计算机发送ICMP协议的ping数据包
pr = p.Send(pAddr);
lost += pr.Status == IPStatus.Success ? 0 :1; // 如果ping成功 , pr.Status == IPStatus.Success
rtt += pr.RoundtripTime;
Console.WriteLine("来自:"+pr.Address + " 字节:" + pr.Buffer.Length + " 时间" + pr.RoundtripTime + "ms 状态:" + pr.Status);
Thread.Sleep(2000);//等待2秒做下一次ping
}
Console.WriteLine("测评:\r\nPing:" + pAddr + " 平均延时:" + rtt / 4 + "ms 丢包率:" +lost+"/4");