C#实现简单的 Ping 的功能,用于测试网络是否已经联通

 1  /// <summary>
 2          /// 是否能 Ping 通指定的主机
 3          /// </summary>
 4          /// <param name="ip">ip 地址或主机名或域名</param>
 5          /// <returns>true 通,false 不通</returns>
 6          public bool Ping(string ip)
 7          {
 8              System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
 9              System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
10              options.DontFragment = true;
11              string data = "Test Data!";
12              byte[] buffer = Encoding.ASCII.GetBytes(data);
13              int timeout = 1000; // Timeout 时间,单位:毫秒
14              System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
15              if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
16                  return true;
17              else
18                  return false;
19          }
View Code

http://www.cnblogs.com/anjou/archive/2007/10/11/920214.html

posted @ 2013-12-17 15:16  天王星天  阅读(484)  评论(0编辑  收藏  举报