代码改变世界

c#下实现ping操作

  观海看云  阅读(557)  评论(0编辑  收藏  举报

C# ping命令实现:利用c#2.0新增的Ping类


这里我写的是一个窗体程序。首先添加textbox,listbox,button控件,其中textbox录入域名或IP,listbox显示结果.


在button1_click事件键入

  1. private void button1_Click(object sender, EventArgs e)   
  2. {   
  3.     Ping p1 = new Ping(); //只是演示,没有做错误处理   
  4.     PingReply reply = p1.Send(this.textBox1.Text);//阻塞方式   
  5.     displayReply(reply); //显示结果   
  6.  
  7. }   
  8.  
  9. private void displayReply(PingReply reply) //显示结果   
  10. {   
  11.     StringBuilder sbuilder ;   
  12.     if (reply.Status == IPStatus.Success)   
  13.     {   
  14.         sbuilder = new StringBuilder();   
  15.         sbuilder.Append(string.Format("Address: {0} ", reply.Address.ToString ()));   
  16.         sbuilder.Append(string.Format("RoundTrip time: {0} ", reply.RoundtripTime));   
  17.         sbuilder.Append(string.Format("Time to live: {0} ", reply.Options.Ttl));   
  18.         sbuilder.Append(string.Format("Don't fragment: {0} ", reply.Options.DontFragment));   
  19.         sbuilder.Append(string.Format("Buffer size: {0} ", reply.Buffer.Length));   
  20.         listBox1.Items.Add(sbuilder.ToString());   
  21.     }   
  22.  
  23. }  

也可以做异步的处理,修改button1_click,并添加PingCompletedCallBack方法

  1. private void button1_Click(object sender, EventArgs e)   
  2. {   
  3.     Ping p1 = new Ping();   
  4.     p1.PingCompleted += new PingCompletedEventHandler(this.PingCompletedCallBack);//设置PingCompleted事件处理程序   
  5.     p1.SendAsync(this.textBox1.Text, null);   
  6. }  
  7.  
  8. private void PingCompletedCallBack(object sender, PingCompletedEventArgs e)   
  9. {   
  10.     if (e.Cancelled)   
  11.     {   
  12.         listBox1.Items.Add("Ping Canncel");   
  13.         return;   
  14.     }   
  15.     if (e.Error != null)   
  16.     {   
  17.         listBox1.Items.Add(e.Error.Message);   
  18.         return;   
  19.  
  20.     }   
  21.     StringBuilder sbuilder;   
  22.     PingReply reply = e.Reply;   
  23.     if (reply.Status == IPStatus.Success)   
  24.     {   
  25.         sbuilder = new StringBuilder();   
  26.         sbuilder.Append(string.Format("Address: {0} ", reply.Address.ToString()));   
  27.         sbuilder.Append(string.Format("RoundTrip time: {0} ", reply.RoundtripTime));   
  28.         sbuilder.Append(string.Format("Time to live: {0} ", reply.Options.Ttl));   
  29.         sbuilder.Append(string.Format("Don't fragment: {0} ", reply.Options.DontFragment));   
  30.         sbuilder.Append(string.Format("Buffer size: {0} ", reply.Buffer.Length));   
  31.         listBox1.Items.Add(sbuilder.ToString());   
  32.  
  33.     }   
  34. }  
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示