• C# Ping类的使用


    花了一天,随便写的个小程序,源码公开,有需要的可以 拿去。我写这个程序的本意是可以ping有固定IP的圈存机,如何网络不通会自己返回其相应的位置。

    程序说明,程序可以自动ping预先写入的一组IP地址,也可以手动ping输入的ip.

    代码如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.Net.Sockets;
     10 using System.Net;
     11 using System.Net.NetworkInformation;
     12 
     13 namespace Ping_test
     14 {
     15     public partial class ManiForm : Form
     16     {
     17         public ManiForm()
     18         {
     19             InitializeComponent();
     20 
     21         }
     22         //表示可变字符串
     23      
     24      //   IcmpPacket packet = new IcmpPacket(0, 0, 0, 45, 0, 4);
     25       //  Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
     26 
     27         private void button1_Click(object sender, EventArgs e)
     28         {
     29             try
     30             {
     31                 IPAddress myIP = IPAddress.Parse(textBox1.Text);
     32             }
     33             catch(Exception ex)
     34             {
     35                 MessageBox.Show("不是有效的IP");
     36                 return;
     37             }
     38             this.Text = "正在ping....";
     39          //   listBox1.Items.Clear();
     40             Ping pingSender = new Ping();
     41             PingOptions options = new PingOptions();
     42 
     43             // Use the default Ttl value which is 128,
     44             // but change the fragmentation behavior.
     45             options.DontFragment = true;
     46 
     47             // Create a buffer of 32 bytes of data to be transmitted.
     48             string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
     49             byte[] buffer = Encoding.ASCII.GetBytes(data);
     50             int timeout = 120;
     51             for (int i = 0; i < 4; i++)
     52             {
     53                 PingReply reply = pingSender.Send(textBox1.Text, timeout, buffer, options);      
     54                 displayReply(reply);
     55                 timeDelay(800);    //0.8s    
     56           
     57              }
     58             this.Text = "已完成";
     59         }
     60         private void button3_Click(object sender, EventArgs e)
     61         {
     62             this.Close();
     63             Application.Exit(); 
     64         }
     65       
     66 
     67         private void button2_Click(object sender, EventArgs e)
     68         {
     69             string[] ipaddress = new string[4];
     70             ipaddress[0] = "192.168.1.1";
     71             ipaddress[1] = "192.168.1.2";
     72             ipaddress[2] = "192.168.1.3";
     73             ipaddress[3] = "192.168.1.4";
     74             for (int i = 0; i < 4; i++)
     75             {
     76                 textBox1.Text = ipaddress[i];
     77                 button1_Click(this, e);
     78             }
     79 
     80         }
     81         private void displayReply(PingReply reply) //显示结果   
     82         {
     83             StringBuilder sbuilder;
     84             sbuilder = new StringBuilder();
     85             if (reply.Status == IPStatus.Success)
     86             {
     87                     
     88                 sbuilder.Append(string.Format("地址: {0} ", reply.Address.ToString()));
     89                 sbuilder.Append(string.Format("时间: {0} ", reply.RoundtripTime));
     90                 sbuilder.Append(string.Format("生存期: {0} ", reply.Options.Ttl));
     91              // sbuilder.Append(string.Format("Don't fragment: {0} ", reply.Options.DontFragment));
     92                 sbuilder.Append(string.Format("缓存: {0} ", reply.Buffer.Length));
     93                 listBox1.Items.Add(sbuilder.ToString());   
     94             }
     95             else
     96             {
     97                 
     98                 switch(textBox1.Text)
     99                 {
    100                     case "192.168.1.1":
    101                         {
    102                           sbuilder.Append("地址:"+textBox1.Text + "  一号圈存机不可达!");
    103                           listBox1.Items.Add(sbuilder.ToString());
    104                           break;
    105                         }
    106                     case "192.168.1.2":
    107                         {
    108                             sbuilder.Append("地址:"+textBox1.Text + "  二号圈存机不可达!");
    109                             listBox1.Items.Add(sbuilder.ToString());
    110                             break;
    111                         }
    112                     case "192.168.1.3":
    113                         {
    114                             sbuilder.Append("地址:"+textBox1.Text + "  三号圈存机不可达!");
    115                             listBox1.Items.Add(sbuilder.ToString());
    116                             break;
    117                         }
    118                 case "192.168.1.4":
    119                     {
    120                         sbuilder.Append("地址:"+textBox1.Text + "  四号圈存机不可达!");
    121                         listBox1.Items.Add(sbuilder.ToString());
    122                         break;
    123                     }
    124 
    125                   }   //switch
    126 
    127 
    128             }
    129             this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
    130         }
    131         /// <summary>
    132         /// 延时函数
    133         /// </summary>
    134         /// <param name="iInterval"></param>
    135         private void timeDelay(int iInterval)
    136         {
    137             DateTime now = DateTime.Now;
    138             while (now.AddMilliseconds(iInterval) > DateTime.Now)
    139             { }
    140             return;
    141         }
    142 
    143         private void button4_Click(object sender, EventArgs e)
    144         {
    145             this.listBox1.Items.Clear();
    146         }
    147 
    148     
    149 
    150     }
    151 }
  • 相关阅读:
    .NET CF 枚举设备窗口
    .NET CF WM设备(手机)振动
    如何将 byte[] 转换为 IntPtr?
    Mobile 重启设备
    如何删除只读文件?
    随笔
    故乡的原风景
    岁月神偷
    opengl纹理映射
    bootstrap 学习笔记
  • 原文地址:https://www.cnblogs.com/dreamfactory/p/2695575.html
Copyright © 2020-2023  润新知