C#通过编程方式实现Ping
代码是照着书敲的,贴出来方便平时参考
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.NetworkInformation; namespace _20通过编程方式实现Ping { class Program { static void Main(string[] args) { Ping ping = new Ping(); PingOptions pingOpt = new PingOptions(); pingOpt.DontFragment = true;//是否设置分段数据 string myInfo = "Hello, world!"; byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo); int timeOut = 1200; string ipTarget = "192.168.1.102"; PingReply pingReply = ping.Send(ipTarget, timeOut, bufferInfo); if (pingReply.Status == IPStatus.Success) { Console.WriteLine("耗费时间 - {0}\n路由节点数 - {1}\n数据分段 - {2}\n缓冲区大小 - {3}", pingReply.RoundtripTime, //耗费时间 pingReply.Options.Ttl, //路由节点数 pingReply.Options.DontFragment ? "发生分段" : "没有发生分段",//数据分段 pingReply.Buffer.Length//缓冲区大小 ); } else { Console.WriteLine("无法ping通"); } Console.ReadKey(); } } }
如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)