日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C#實(shí)現(xiàn)網(wǎng)絡(luò)小程序的步驟詳解_C#教程

作者:dawn ? 更新時(shí)間: 2023-02-02 編程語(yǔ)言

經(jīng)常要檢測(cè)某些IP地址范圍段的計(jì)算機(jī)是否在線。

有很多的方法,比如進(jìn)入到網(wǎng)關(guān)的交換機(jī)上去查詢、使用現(xiàn)成的工具或者編寫一個(gè)簡(jiǎn)單的DOS腳本等等,這些都比較容易實(shí)現(xiàn)。

現(xiàn)在使用C#來(lái)完成。

1、簡(jiǎn)單上手

公用函數(shù):

        public static long IPToLong(string ip)
        {
            //將IP地址轉(zhuǎn)換為長(zhǎng)整數(shù)
            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)
        {
            //將長(zhǎng)整數(shù)IP地址轉(zhuǎn)換為實(shí)際的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;
        }

點(diǎn)擊【檢測(cè)】按鈕:

            DateTime startTime, endTime;
            startTime= DateTime.Now;
            listBox1.Items.Add(startTime.ToString());
            string startIP = textBox1.Text;
            string endIP = textBox2.Text;
 
            // 將起始IP地址和結(jié)束IP地址轉(zhuǎn)換為長(zhǎng)整數(shù) 
            long startIPLong = IPToLong(startIP);
            long endIPLong = IPToLong(endIP);
 
            Ping ping = new Ping();
            // 遍歷IP地址范圍 
            for (long i = startIPLong; i <= endIPLong; i++)
            {
                // 將整數(shù)轉(zhuǎn)換為IP地址 
                string ip = LongToIP(i);
 
                // 輸出 
                PingReply pingReply = ping.Send(ip);
                if (pingReply.Status == IPStatus.Success)
                {
                    listBox1.Items.Add( ip + "=>計(jì)算機(jī)在線");
                }
                else
                {
                    listBox1.Items.Add(ip);
                }
 
            }
            endTime = DateTime.Now;
            listBox1.Items.Add(endTime.ToString());
            listBox1.Items.Add("OK:" + (endTime - startTime).ToString());

執(zhí)行時(shí)沒(méi)有問(wèn)題的,可以出來(lái)結(jié)果,問(wèn)題是界面卡頓了。

執(zhí)行的時(shí)間也很長(zhǎng),執(zhí)行需要1分21秒。

這個(gè)結(jié)果不能接受,需要對(duì)程序加以改進(jìn)。

2、使用并行計(jì)算

不需要改動(dòng)公用代碼,只需要改動(dòng)檢測(cè)部分。

            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地址和結(jié)束IP地址轉(zhuǎn)換為整數(shù) 
            long startIPLong = IPToLong(startIP);
            long endIPLong = IPToLong(endIP);
 
            // 創(chuàng)建一個(gè)可以存儲(chǔ)IP地址的List
            List<string> ipList = new List<string>();
 
            for (long i = startIPLong; i <= endIPLong; i++)
            {
                // 將整數(shù)轉(zhuǎn)換為IP地址 
                string ip = LongToIP(i);
                ipList.Add(ip);
                ipResults.Add(ip,"");
                listBox1.Items.Add(ip);
            }
 
            // 開啟并行計(jì)算
            Parallel.ForEach(ipList, (ip) =>
            {
            // 創(chuàng)建一個(gè)新的Ping類
            Ping pingSender = new Ping();
            // 發(fā)送一個(gè)Ping請(qǐng)求
            var reply = pingSender.Send(ip);
 
            // 如果返回的狀態(tài)是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());

增加一個(gè)公用函數(shù):

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

出現(xiàn)問(wèn)題:

⑴ 提示“C#線程間操作無(wú)效:從不是創(chuàng)建控件“textbox1”的線程訪問(wèn)它”,這個(gè)就簡(jiǎn)單設(shè)置Control.CheckForIllegalCrossThreadCalls=false即可解決;

⑵ 提示“l(fā)istbox1包含的項(xiàng)太多”,這個(gè)是不是與并行計(jì)算開啟的線程太多有關(guān)?考慮到本機(jī)是8核,保守一點(diǎn),修改參數(shù)

Parallel.ForEach(ipList, new ParallelOptions() { MaxDegreeOfParallelism = MaxDegreeOfParallelism = Environment.ProcessorCount }, (ip) =>
{
    //執(zhí)行代碼   
}

程序可以正常運(yùn)行了。

⑶ 界面依然阻塞,但用時(shí)更多了。

去ping255個(gè)地址,竟然用了12分32秒!!!這個(gè)真是不可忍受,最關(guān)鍵沒(méi)有界面交互,這太不用戶友好了!

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地址和結(jié)束IP地址轉(zhuǎn)換為整數(shù) 
            long startIPLong = IPToLong(startIP);
            long endIPLong = IPToLong(endIP);
 
            // 創(chuàng)建一個(gè)可以存儲(chǔ)IP地址的List
            List<string> ipList = new List<string>();
 
            for (long i = startIPLong; i <= endIPLong; i++)
            {
                // 將整數(shù)轉(zhuǎn)換為IP地址 
                string ip = LongToIP(i);
                ipList.Add(ip);
                ipResults.Add(ip,"");
                listBox1.Items.Add(ip);
            }
 
            Task task = new Task(() =>
            {
                // 創(chuàng)建一個(gè)多線程
                Parallel.ForEach(ipList, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, (ip) =>
                {
                    // 創(chuàng)建一個(gè)新的Ping類
                    Ping pingSender = new Ping();
                    // 發(fā)送一個(gè)Ping請(qǐng)求
                    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());

好了,因?yàn)槭褂肨ask異步編程,界面也不再阻塞和卡頓了,不過(guò)運(yùn)行多少時(shí)間不能準(zhǔn)確得到,改動(dòng)一下代碼,就是將時(shí)間輸出定位到任務(wù)完成:

            task.ContinueWith((t) =>
            {
                endTime = DateTime.Now;
                listBox1.Items.Add(endTime.ToString());
                listBox1.Items.Add("OK:" + (endTime - startTime).ToString());
            });

運(yùn)行時(shí)間為1分41秒,界面不卡頓了。

可是運(yùn)行仍然是逐個(gè)IP地址在ping,根本沒(méi)有并行計(jì)算的效果。雖然使用了Parallel.ForEach,可能是由于線程需要等待導(dǎo)致資源開銷過(guò)大,那么線程沒(méi)有被有效地利用。

這其實(shí)與普通的使用線程來(lái)做沒(méi)有什么區(qū)別了。

4、補(bǔ)充

⑴ 檢測(cè)方法:

            string ipAddress = (string)obj;
            TcpClient client = new TcpClient();
            try
            {
                client.Connect(ipAddress, 80);
                //在線
            }
            catch (Exception ex)
            {
                //不在線
            }
            finally
            {
                client.Close();
            }

引用:

using System.Net.Sockets;

⑵ 檢測(cè)輸入的IP地址是否合法:

    IPAddress address;
    if (IPAddress.TryParse(ipAddress, out address))
    {
        // 檢查IP地址是否為IPv4地址
        if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            return true;
    }else{
        return false;
    }

對(duì)于檢測(cè)計(jì)算機(jī)在線與否沒(méi)有好的方法?看那么多的網(wǎng)絡(luò)小工具,肯定有!

先完成這個(gè)效果,后面再進(jìn)行優(yōu)化。

原文鏈接:https://blog.csdn.net/dawn0718/article/details/128449738

欄目分類
最近更新