using System.Net.NetworkInformation; /// <summary> /// ping the specified ip /// </summary> /// <param name="ipStr">ip address, "x.x.x.x"</param> /// <returns>if connected, return true, otherwise return false</returns> private bool Ping(string ipStr) { Ping ping = new Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; //测试数据 string data = "test data abcabc"; byte[] buffer = Encoding.ASCII.GetBytes(data); //设置超时时间 int timeout = 120; //调用同步 send 方法发送数据,将返回结果保存至PingReply实例 PingReply reply = ping.Send(ipStr, timeout, buffer, options); if (reply.Status == IPStatus.Success) return true; else return false; }
时间: 2024-12-29 04:52:05