C# 网络测试工具

    xiaoxiao2022-07-13  163

    using System;//From:www.uzhanbao.com using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets;     namespace TestIp {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }           private void btnStart_Click(object sender, EventArgs e)         {             for (int i = 1; i < 250;i++ )             {                 PingIp p = new PingIp(tbIp.Text + "."+i);                 p.pingEvent += new PingIp.PingHandle(p_pingEvent);             }         }           void p_pingEvent(string ip,bool flag)         {             this.BeginInvoke(new MethodInvoker(delegate {                 if (checkBox1.Checked)                 {                     if (flag == true)                     {                         textBox2.Text += ip + " " + flag + "\r\n";                     }                 }                 else                 {                     textBox2.Text += ip + " " + flag + "\r\n";                 }             }));         }           private void Form1_Load(object sender, EventArgs e)         {           }           private void button1_Click(object sender, EventArgs e)         {             textBox2.Text = "";         }     } }  

     

     

     

     

     

    using System; using System.Collections.Generic; using System.Text; using System.Net.NetworkInformation; using System.Threading;   namespace TestIp {     class PingIp     {         Thread thread;         System.Timers.Timer timer;         public string Ip;           public delegate void PingHandle(string ip,bool flag);         public event PingHandle pingEvent;            public PingIp(string ip)         {             Ip = ip;             RunSecondThread();             //thread = new Thread(new ThreadStart(RunSecondThread));             //thread.Start();         }           void RunSecondThread()         {             timer = new System.Timers.Timer(5000);             timer.AutoReset = true;             timer.Enabled = true;             timer.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);         }           void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)         {             AutoPingIP(Ip);         }           void AutoPingIP(string ip)         {             Ping p = new Ping();             PingOptions ops = new PingOptions();             ops.DontFragment = true;             string d = "test data";             byte[] buf = Encoding.ASCII.GetBytes(d);             int timeout = 2000;               PingReply pr = p.Send(ip, timeout, buf, ops);               if (pr.Status == IPStatus.Success)             {                 if (pingEvent != null)                     pingEvent(ip,true);             }             else             {                 if (pingEvent != null)                     pingEvent(ip,false);             }             StopTh();         }           public void StopTh()         {             if (timer != null)             {                 timer.Dispose();             }             if (thread != null)             {                 if (thread.IsAlive)                     thread.Abort();             }         }     } }

    效果图:

     

     

    ----------------------  这个更简单--------------------------

    using System.Net.NetworkInformation;         public bool PingIpFun(string ip)         {             bool online = false; //是否在线             Ping ping = new Ping();             PingReply pingReply = ping.Send(ip);                          if (pingReply.Status == IPStatus.Success)             {                 online = true;                // Console.WriteLine("当前在线,已ping通!");             }             else             {                // Console.WriteLine("不在线,ping不通!");             }              return online;           }

    最新回复(0)