软件编程
位置:首页>> 软件编程>> C#编程>> C#实现网络小程序的步骤详解

C#实现网络小程序的步骤详解

作者:dawn  发布时间:2023-08-17 18:16:37 

标签:C#,网络,小程序

经常要检测某些IP地址范围段的计算机是否在线。

有很多的方法,比如进入到网关的交换机上去查询、使用现成的工具或者编写一个简单的DOS脚本等等,这些都比较容易实现。

现在使用C#来完成。

1、简单上手

公用函数:

public static long IPToLong(string ip)
       {
           //将IP地址转换为长整数
           string[] ipBytes = ip.Split('.');
           long ipLong = 0;
           ipLong += long.Parse(ipBytes[0]) * 256 * 256 * 256;
           ipLong += long.Parse(ipBytes[1]) * 256 * 256;
           ipLong += long.Parse(ipBytes[2]) * 256;
           ipLong += long.Parse(ipBytes[3]);
           return ipLong;
       }

public static string LongToIP(long ip)
       {
           //将长整数IP地址转换为实际的IP地址
           long ipLong = ip;
           string ipString = string.Empty;
           ipString += ipLong / 256 / 256 / 256 + ".";
           ipLong = ipLong % (256 * 256 * 256);
           ipString += ipLong / 256 / 256 + ".";
           ipLong = ipLong % (256 * 256);
           ipString += ipLong / 256 + ".";
           ipLong = ipLong % 256;
           ipString += ipLong;
           return ipString;
       }

点击【检测】按钮:

DateTime startTime, endTime;
           startTime= DateTime.Now;
           listBox1.Items.Add(startTime.ToString());
           string startIP = textBox1.Text;
           string endIP = textBox2.Text;

// 将起始IP地址和结束IP地址转换为长整数
           long startIPLong = IPToLong(startIP);
           long endIPLong = IPToLong(endIP);

Ping ping = new Ping();
           // 遍历IP地址范围
           for (long i = startIPLong; i <= endIPLong; i++)
           {
               // 将整数转换为IP地址
               string ip = LongToIP(i);

// 输出
               PingReply pingReply = ping.Send(ip);
               if (pingReply.Status == IPStatus.Success)
               {
                   listBox1.Items.Add( ip + "=>计算机在线");
               }
               else
               {
                   listBox1.Items.Add(ip);
               }

}
           endTime = DateTime.Now;
           listBox1.Items.Add(endTime.ToString());
           listBox1.Items.Add("OK:" + (endTime - startTime).ToString());

执行时没有问题的,可以出来结果,问题是界面卡顿了。

C#实现网络小程序的步骤详解

执行的时间也很长,执行需要1分21秒。

这个结果不能接受,需要对程序加以改进。

2、使用并行计算

不需要改动公用代码,只需要改动检测部分。

Dictionary<string, string> ipResults = new Dictionary<string, string>();

DateTime startTime, endTime;
           startTime = DateTime.Now;
           listBox1.Items.Add(startTime.ToString());

string startIP = textBox1.Text;
           string endIP = textBox2.Text;

// 将起始IP地址和结束IP地址转换为整数
           long startIPLong = IPToLong(startIP);
           long endIPLong = IPToLong(endIP);

// 创建一个可以存储IP地址的List
           List<string> ipList = new List<string>();

for (long i = startIPLong; i <= endIPLong; i++)
           {
               // 将整数转换为IP地址
               string ip = LongToIP(i);
               ipList.Add(ip);
               ipResults.Add(ip,"");
               listBox1.Items.Add(ip);
           }

// 开启并行计算
           Parallel.ForEach(ipList, (ip) =>
           {
           // 创建一个新的Ping类
           Ping pingSender = new Ping();
           // 发送一个Ping请求
           var reply = pingSender.Send(ip);

// 如果返回的状态是Success,则IP地址在线
           if (reply.Status == IPStatus.Success)
           {
               ipResults[ip] = "在线";
               ChangeIPStatus(ip,"在线");
               }
               else
               {
                   ipResults[ip] = "NO";
                   ChangeIPStatus(ip, "NO");
               }
           });

endTime = DateTime.Now;
           listBox1.Items.Add(endTime.ToString());
           listBox1.Items.Add("OK:" + (endTime - startTime).ToString());

增加一个公用函数:

public void ChangeIPStatus(string ip,string S1)
       {
           // 定位到listbox中记录并更改IP是否在线
           int index = listBox1.Items.IndexOf(ip);
           listBox1.Items[index] = ip+""+S1;
       }

出现问题:

⑴提示&ldquo;C#线程间操作无效:从不是创建控件&ldquo;textbox1&rdquo;的线程访问它&rdquo;,这个就简单设置Control.CheckForIllegalCrossThreadCalls=false即可解决;

⑵提示&ldquo;listbox1包含的项太多&rdquo;,这个是不是与并行计算开启的线程太多有关?考虑到本机是8核,保守一点,修改参数

Parallel.ForEach(ipList, new ParallelOptions() { MaxDegreeOfParallelism = MaxDegreeOfParallelism = Environment.ProcessorCount }, (ip) =>
{
   //执行代码  
}

程序可以正常运行了。

⑶界面依然阻塞,但用时更多了。

C#实现网络小程序的步骤详解

去ping255个地址,竟然用了12分32秒!!!这个真是不可忍受,最关键没有界面交互,这太不用户友好了!

3、添加异步编程

Dictionary<string, string> ipResults = new Dictionary<string, string>();

DateTime startTime, endTime;
           startTime = DateTime.Now;
           listBox1.Items.Add(startTime.ToString());

string startIP = textBox1.Text;
           string endIP = textBox2.Text;

// 将起始IP地址和结束IP地址转换为整数
           long startIPLong = IPToLong(startIP);
           long endIPLong = IPToLong(endIP);

// 创建一个可以存储IP地址的List
           List<string> ipList = new List<string>();

for (long i = startIPLong; i <= endIPLong; i++)
           {
               // 将整数转换为IP地址
               string ip = LongToIP(i);
               ipList.Add(ip);
               ipResults.Add(ip,"");
               listBox1.Items.Add(ip);
           }

Task task = new Task(() =>
           {
               // 创建一个多线程
               Parallel.ForEach(ipList, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, (ip) =>
               {
                   // 创建一个新的Ping类
                   Ping pingSender = new Ping();
                   // 发送一个Ping请求
                   var reply = pingSender.Send(ip);

if (reply.Status == IPStatus.Success)
                   {
                       listBox1.Invoke(new Action(() =>
                       {
                           int index = listBox1.Items.IndexOf(ip);
                           listBox1.Items[index] = ip + "" + "在线";
                       }));
                   }
                   else
                   {
                       listBox1.Invoke(new Action(() =>
                       {
                           int index = listBox1.Items.IndexOf(ip);
                           listBox1.Items[index] = ip + "" + "NO";
                       }));
                   }

});
           });

task.Start();
           Task.WaitAll();
           endTime = DateTime.Now;
           listBox1.Items.Add(endTime.ToString());
           listBox1.Items.Add("OK:" + (endTime - startTime).ToString());

来源:https://blog.csdn.net/dawn0718/article/details/128449738

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com