C# 多工網段掃描練習

diligentyang發表於2016-09-25

建立一個WPF應用程式,用多工來掃描一個網段內的計算機,根據計算機的IP地址獲取其主機名,程式執行結果如下:

示例

示例

示例

廢話不多說,直接上程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace A._2
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        int n = 0;
        DateTime taskBegin = DateTime.Now;
        public MainWindow()
        {
            InitializeComponent();
            textBox_first.Text = "192.168.1.";
            textBox_start.Text = "102";
            textBox_end.Text = "105";
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            IPAddress ipstart;
            IPAddress ipend;
            if (IPAddress.TryParse(textBox_first.Text + textBox_start.Text, out ipstart)&& IPAddress.TryParse(textBox_first.Text + textBox_end.Text, out ipend))
            {
                errorLable.Visibility = Visibility.Hidden;
            }
            else
            {
                errorLable.Visibility = Visibility.Visible;
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            IPAddress ipstart;
            IPAddress ipend;
            listBox.Items.Clear();
            n = int.Parse(textBox_end.Text) - int.Parse(textBox_start.Text) + 1;
            if (IPAddress.TryParse(textBox_first.Text + textBox_start.Text, out ipstart) && IPAddress.TryParse(textBox_first.Text + textBox_end.Text, out ipend))
            {
                errorLable.Visibility = Visibility.Hidden;
                for (int i= int.Parse(textBox_start.Text); i<=int.Parse(textBox_end.Text);i++){
                    IPAddress ip = IPAddress.Parse(textBox_first.Text+i.ToString());
                    Thread t = new Thread(scan);
                    t.Start(ip);
                }
            }
            else
            {
                errorLable.Visibility = Visibility.Visible;
            }
        }

        private void scan(Object ip)
        {
            DateTime begin = DateTime.Now;
            IPAddress ipAddress = (IPAddress)ip;
            string hostName;
            try
            {
                hostName = Dns.GetHostEntry(ipAddress).HostName;
            }
            catch
            {
                hostName = "(不線上)";
            }
            DateTime end = DateTime.Now;
            TimeSpan ts = end - begin;
            listBox.Dispatcher.Invoke(() => listBox.Items.Add("掃描地址:"+ ipAddress.ToString()+" 掃描用時:"+ ts.TotalMilliseconds+ "毫秒 " + " 主機名稱:" +hostName));
            n--;
            if (n == 0) {
                DateTime taskEnd = DateTime.Now;
                TimeSpan taskAll = taskEnd - taskBegin;
                MessageBox.Show("掃描完畢!總用時:"+ taskAll.TotalMilliseconds + "毫秒");
            }

        }
    }
}

此程式還可以細緻化一下,比如,起始值不能大於終止值,地址字首後面的點,程式可以自動補全,錯誤提示細緻化等等。

相關文章