A.2多任务网段扫描练习

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_35252878/article/details/60781383

对于《C#网络应用编程》(人民邮电出版社 马骏主编) 课后上机练习A.2的实现与扩展

题目要求达到的效果:
这里写图片描述

增加功能:
1、如果192.168.1.的输入后面少了一个.号,系统自动补上。
2、线程结束后,弹出对话框:“扫描结束”。
3、统计并显示最大时间和最小时间。

这里写图片描述

这里写图片描述
xaml:

<Window x:Class="A._2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:A._2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Height="320" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBox HorizontalAlignment="Left" Height="23" Name="Textbox" Visibility="Hidden"  TextWrapping="Wrap" Text="                                       IP地址有错,请更正!" VerticalAlignment="Top" Width="495" Background="Red" Margin="10,119,0,0" TextChanged="Textbox_TextChanged_1"/>
        <GroupBox x:Name="groupBox" Header="扫描的IP地址" HorizontalAlignment="Left" Margin="10,10,0,203" Width="497">
            <Grid HorizontalAlignment="Left" Height="106" Margin="1,-9,-3,-12" VerticalAlignment="Top" Width="487">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="135*"/>
                    <ColumnDefinition Width="136*"/>
                    <ColumnDefinition Width="133*"/>
                    <ColumnDefinition Width="83*"/>
                </Grid.ColumnDefinitions>
                <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="61,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="67" TextChanged="textBox_TextChanged"/>
                <TextBox x:Name="textBox1" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="58,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="75"/>
                <TextBox x:Name="textBox2" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="55,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="67"/>
                <Button x:Name="button" Content="开始扫描" Grid.Column="3" HorizontalAlignment="Left" Margin="10,25,0,0" VerticalAlignment="Top" Width="63" Height="22" Click="button_Click"/>
            </Grid>
        </GroupBox>
        <Label x:Name="label" Content="地址前缀:" HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Height="26" Width="68"/>
        <Label x:Name="label2" Content="起始值:" HorizontalAlignment="Left" Margin="150,39,0,0" VerticalAlignment="Top" Height="26" Width="58"/>
        <Label x:Name="label3" Content="终止值:" HorizontalAlignment="Left" Margin="293,39,0,0" VerticalAlignment="Top" Height="26" Width="58"/>
        <GroupBox x:Name="groupBox1" Header="扫描信息" HorizontalAlignment="Left" Margin="10,139,0,0" VerticalAlignment="Top" Height="171" Width="497">
            <ListBox HorizontalAlignment="Left" Name="ListBox" Height="141" VerticalAlignment="Top" Width="487" Margin="0,10,-2.2,-2.2"/>
        </GroupBox>
    </Grid>
</Window>

MainWindow.xaml.cs:

namespace A._2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        int num;
        long minTime = 0xffff;
        long maxTime = 0;
        private void judge_IPAddress(object Address)
        {
            num--;
            string Dname;
            String address=(string)Address;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                IPAddress ip = IPAddress.Parse(address);
                Dname = Dns.GetHostEntry(ip).HostName;
            }
            catch
            {
                Dname = "(不在线)";
            }
            sw.Stop();
            if (sw.ElapsedMilliseconds > maxTime)
                maxTime = sw.ElapsedMilliseconds;
            if (sw.ElapsedMilliseconds < minTime)
                minTime = sw.ElapsedMilliseconds;
            ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("扫描地址:" + Address + ",扫描用时:" + sw.ElapsedMilliseconds + "毫秒,主机DNS名称:" + Dname); }));
           // ListBox.Items.Add("扫描地址:" + Address + ",扫描用时:"+sw.ElapsedMilliseconds+"毫秒,主机DNS名称:"+Dname);
            //if(num==0)
            //    ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("扫描结束!"); }));
            Thread.Sleep(1000);
        }

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            string address;
            string add = textBox.Text;
            int l = add.Length;
            if(add[l-1]!='.')
            {
                add = add + '.';
            }
            Textbox.Foreground = new SolidColorBrush(Colors.White); 
            num = Int32.Parse(textBox2.Text) - Int32.Parse(textBox1.Text);
            if (Int32.Parse(textBox1.Text)>255)
            {
                Textbox.Visibility = Visibility.Visible;
                return;
            }
            for(int i= Int32.Parse(textBox1.Text);i<= Int32.Parse(textBox2.Text);i++)
            {
                if (i == Int32.Parse(textBox2.Text))
                    MessageBox.Show("扫描结束!");
                address = add + i.ToString();
                try
                {
                    IPAddress ip = IPAddress.Parse(address);
                }
                catch
                {
                    Textbox.Visibility = Visibility.Visible;
                    return;
                }
                Thread t = new Thread(judge_IPAddress);
                t.Start(address);

                if (i == Int32.Parse(textBox2.Text))
                {
                    t.Abort();
                    ListBox.Dispatcher.Invoke(new Action(() => { ListBox.Items.Add("最长用时:"+maxTime+"s,最短用时:"+minTime+"s.");}));
                    MessageBox.Show("扫描结束!");
                    break;
                }
            }

        }

        private void Textbox_TextChanged_1(object sender, TextChangedEventArgs e)
        {
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页