实验二 |
||||
姓名: |
学号:212 |
专业年级: 2021级 |
班级:软1 |
|
分组: |
实验室: |
指导教师: |
实验日期: |
|
实验的准备阶段 (指导教师填写) |
课程名称 |
网络应用编程 |
||
实验名称 |
实验2-多任务网段扫描实验 |
|||
实验目的 |
熟悉和掌握任务的创建、异步关键字等异步编程技术。 |
|||
实验内容 |
创建一个WPF应用程序,用多任务来扫描一个网段内的计算机,根据计算机的IP地址获取其主机域名,程序运行效果如下图所示。 正在上传…重新上传取消 |
|||
实验类型 (打R) |
□验证性 □演示性 □设计性 R综合性 |
|||
实验的重点、难点 |
重点:任务的创建方法和使用。 难点:Async和await异步关键字。 |
|||
实验环境 |
硬件:普通台式机或笔记本电脑;软件:Visual Studio 2012以上版本。 |
|||
实验的实施阶段 |
实验步骤及实验结果 |
(请同学们详细描述:1.程序设计的思路;2.试验方案的设计;3.使用的技术方法和属性;4.粘贴关键代码;5.程序运行的结果) MainWindow.xaml <Window x:Class="WpfApp3.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:WpfApp3" mc:Ignorable="d" Title="MainWindow" Height="450" Width="500" WindowStartupLocation="CenterScreen"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition /> </Grid.RowDefinitions> <GroupBox Header="扫描的ip地址范围" Grid.Row="0" Padding="10"> <DockPanel> <DockPanel.Resources> <Style TargetType="TextBox"> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="Padding" Value="5 0 5 0"/> <EventSetter Event="TextChanged" Handler="TextBox_TextChanged"/> </Style> </DockPanel.Resources> <Label DockPanel.Dock="Left" Content="地址前缀:" /> <TextBox Name="txt1" Text="192.168.1."/> <Label Content="起始值:" Margin="15 0 0 0"/> <TextBox Name="txt2" Text="103"/> <Label Content="终止值:" Margin="15 0 0 0"/> <TextBox Name="txt3" Text="105"/> <Button Name="btn1" Content="开始扫描" Width="70" Click="btn1_Click"/> </DockPanel> </GroupBox> <Label Grid.Row="1" Content="IP地址有错,请更正!" HorizontalContentAlignment="Center" Name="lb_Error" Background="red" Foreground="White"/> <GroupBox Grid.Row="2" Header="扫描信息"> <ListBox Name="listbox"/> </GroupBox>
</Grid> </Window> 正在上传…重新上传取消 MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Text; 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 WpfApp3 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); lb_Error.Visibility = Visibility.Collapsed; } private void btn1_Click(object sender, RoutedEventArgs e) { int start = int.Parse(txt2.Text); int end = int.Parse(txt3.Text); if(start > end) { MessageBox.Show("终止值必须大于起始值", "地址范围错误"); return; } listbox.Items.Clear(); for(int i = start; i <= end; i++) { IPAddress ip=IPAddress.Parse(txt1.Text+i); Task.Run(() => Scan(ip)); } } private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { string s1=txt1.Text+txt2.Text; string s2=txt1.Text+txt3.Text; IPAddress ip1, ip2; if (IPAddress.TryParse(s1,out ip1) == false || IPAddress.TryParse(s2,out ip2) == false) { lb_Error.Visibility=Visibility.Visible; btn1.IsEnabled=false; } else { lb_Error.Visibility=Visibility.Collapsed; btn1.IsEnabled=true; } } private void Scan(IPAddress ip) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); string name; try { name = Dns.GetHostEntry(ip).HostName; } catch { name = "(不在线)"; } stopwatch.Stop(); listbox.Dispatcher.Invoke( ()=>listbox.Items.Add(string.Format("扫描地址:{0},扫描用时:{1}毫秒,主机DNS名称:{2}" ,ip,stopwatch.ElapsedMilliseconds,name))); } } } |
||
实验结果的处理阶段 |
实验结果的分析与总结 |
程序初始运行页面: 正在上传…重新上传取消 |
||
当地址输入错误时: 正在上传…重新上传取消 起始值大于终止值时: 正在上传…重新上传取消 正常扫描: 正在上传…重新上传取消 |
多任务网段扫描实验
猜你喜欢
转载自blog.csdn.net/qq_64628470/article/details/129927354
今日推荐
周排行