C# 多任务网段扫描练习

创建一个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 + "毫秒");
            }

        }
    }
}

此程序还可以细致化一下,比如,起始值不能大于终止值,地址前缀后面的点,程序可以自动补全,错误提示细致化等等。

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值