多任务网段扫描
--
---使用动态布局(优点:界面布局清晰、控件会随着窗口大小而改变)
<Grid.RowDefinitions>
<RowDefinition Height="1"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Header="扫描的IP地址范围" Grid.Row="0" Grid.RowSpan="3" FontSize="15px" Margin="0,2,0,-63">
<Canvas Height="48" VerticalAlignment="Bottom">
<Label Content="地址前缀:" Width="80" Canvas.Top="11"/>
<TextBox x:Name="tb1" Height="25" Width="124"Canvas.Left="80" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Label Content="起始值:" Width="63" Canvas.Top="11" Canvas.Left="209"/>
<TextBox Name="tb2" Height="25" Width="77" Canvas.Left="277" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Label Content="终止值:" Width="58" RenderTransformOrigin="3.888,1.889" Canvas.Left="359" Canvas.Top="11"/>
<TextBox Name="tb3" Height="25" Width="97" Canvas.Left="422" Canvas.Top="11" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Name="btn1" Width="75" Content="开始扫描" Canvas.Left="542" Canvas.Top="10" Height="28" Click="btn1_Click"/>
<Button Name="time_get" Width="93" Content="获取总时间" Click="time_get_Click" Canvas.Left="622" Canvas.Top="10" Height="28"/>
</Canvas>
</GroupBox>
<TextBlock Grid.Row="2" Name="textblock1" Margin="10,74,10,-103" FontSize="30" Width="674" VerticalAlignment="Center" TextAlignment="Center"/>
<GroupBox Header="扫描信息" Grid.Row="2" Margin="0,114,0,-340" FontSize="15px">
<ListBox Margin="0,0,0,10" Name="listbox1">
</ListBox>
</GroupBox>
---
---定义全局变量 所定义的时间变量sumTime必须为长整形,若定义为int,因为扫面的时间可能太大会超出范围
long sumTime = 0;
DateTime times = DateTime.Now;
---扫描线程的点击事件
private void btn1_Click(object sender, RoutedEventArgs e)
{
IPAddress ipStart
IPAddress ipStop
listbox1.Items.Clear()
if (int.Parse(tb3.Text) < int.Parse(tb2.Text))
{
MessageBox.Show("提示:终止值必须大于起始值!")
tb2.Text = ""
tb3.Text = ""
}
try
{
textblock1.Text = null
textblock1.Background = Brushes.AliceBlue
ipStart = IPAddress.Parse(tb1.Text + tb2.Text)
ipStop = IPAddress.Parse(tb1.Text + tb3.Text)
}
catch
{
textblock1.Background = Brushes.Red
textblock1.Foreground = Brushes.White
textblock1.Text = "IP地址有错,请更正!"
}
if (IPAddress.TryParse(tb1.Text + tb2.Text, out ipStart) && IPAddress.TryParse(tb1.Text + tb3.Text, out ipStop))
{
textblock1.Text = null
textblock1.Background = Brushes.AliceBlue
for (int i = int.Parse(tb2.Text)
{
IPAddress ip = IPAddress.Parse(tb1.Text + i.ToString())
//非多线程(若采用多线程则将下面多线程代码注释,将此行代码截除注释即可)
//多线程
Thread t = new Thread(myMainTask)
t.Start(ip)
}
}
else
{
textblock1.Background = Brushes.Red
textblock1.Foreground = Brushes.White
textblock1.Text = "IP地址有错,请更正!"
}
}
---线程执行的方法体
private void myMainTask(Object ip)
{
string hostName
DateTime begin = DateTime.Now
Stopwatch stop = new Stopwatch()
stop.Start()
IPAddress ipAddress = (IPAddress)ip
try
{
hostName = Dns.GetHostEntry(ipAddress).HostName
}
catch
{
hostName = "(不在线)"
}
stop.Stop()
DateTime end = DateTime.Now
TimeSpan ts = end - begin
sumTime += stop.ElapsedMilliseconds
listbox1.Dispatcher.Invoke(() => listbox1.Items.Add("扫描地址:" + ipAddress.ToString() + " , 扫描用时:" + ts.TotalMilliseconds + "毫秒 , " + " 主机DNS名称:" + hostName))
}
---计算总扫描时间的点击事件
private void time_get_Click(object sender, RoutedEventArgs e)
{
textblock1.Text = "扫描总线程所用总时间为:" + sumTime.ToString() + "毫秒";
}

