PING 检测延迟
2020-08-19 本文已影响0人
久百一
public static int GetDey(string ipStr)
{
//构造Ping实例bai
Ping pingSender = new Ping();
//Ping 选项设置
PingOptions options = new PingOptions();
options.DontFragment = true;
//测试数据du
string data = "";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//设置超时时间zhi
int timeout = 120;
//调用同步 send 方法发送数据,将返回结dao果保存至PingReply实例
PingReply reply = pingSender.Send(ipStr, timeout, buffer, options);
int delayTime = 0;
if (reply.Status == IPStatus.Success)
{
delayTime = (int)reply.RoundtripTime;
}
else
delayTime = -1;
return delayTime;
}