咨询热线:0731-58389610
咨询邮箱:2640248354@qq.com
用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通
  • ///   
  •         /// 是否能 Ping 通指定的主机  
  •         ///   
  •         /// ip 地址或主机名或域名  
  •         /// true 通,false 不通  
  •         public bool Ping(string ip)  
  •         {  
  •             System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();  
  •             System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();  
  •             options.DontFragment = true;  
  •             string data = "Test Data!";  
  •             byte[] buffer = Encoding.ASCII.GetBytes(data);  
  •             int timeout = 1000; // Timeout 时间,单位:毫秒  
  •             System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);  
  •             if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)  
  •                 return true;  
  •             else  
  •                 return false;  
  •         }