首页 技术 正文
技术 2022年11月19日
0 收藏 864 点赞 2,947 浏览 2520 个字

c#端口扫描器wpf+socket

布局如下

<Window x:Class="PortTest.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:PortTest"
mc:Ignorable="d"
Title="PortScan" Height="" Width="">
<Grid>
<Menu>
<MenuItem Header="About" FontSize="" Name="About" Click="About_Click"/>
</Menu>
<StackPanel Margin="">
<WrapPanel>
<Label FontSize="" >开始ip</Label>
<TextBox Width="" Name="startIP" FontSize="" VerticalAlignment="Center"></TextBox>
<Label FontSize="">开始端口</Label>
<TextBox Width="" Name="startPort" PreviewTextInput="Number_PreviewTextInput" FontSize="" VerticalAlignment="Center"></TextBox>
</WrapPanel>
<WrapPanel Margin="0 10">
<Label FontSize="">结束ip</Label>
<TextBox Width="" Name="endIP" GotFocus="endIP_GotFocus" FontSize="" VerticalAlignment="Center"></TextBox>
<Label FontSize="">结束端口</Label>
<TextBox Width="" Name="endPort" PreviewTextInput="Number_PreviewTextInput" GotFocus="endPort_GotFocus" FontSize="" VerticalAlignment="Center"></TextBox>
</WrapPanel>
<WrapPanel>
<Label FontSize="">超时时长(ms)</Label>
<TextBox Name="timeOut" PreviewTextInput="Number_PreviewTextInput" FontSize="" VerticalAlignment="Center" Width=""></TextBox>
</WrapPanel>
<Button Click="Btn_Test" Width="" Height="" Margin="276,15">开始测试</Button>
<ListView Name="resultListView" Height=""></ListView>
</StackPanel>
</Grid>
</Window>

这里有两个比较关键的功能,将ip字符串转成对应的ulong值,同时还有逆过程

大概思路为

ip:127.0.0.1

127*256*256*256+0*256*256+0*256+1

逆过程则为先%256获取最后一位的值,然后减去这个值再进行模运算,可参考代码进行理解,如下

public static ulong IP2ULong(string ip)
{
if (!CheckIP(ip))
{
//ip error
MessageBox.Show("ip错误");
}
List<ulong> data = new List<ulong>();
var ips = ip.Split('.');
foreach (var item in ips)
{
data.Add(ulong.Parse(item));
}
ulong result = ;
ulong first = data[] * * * ;
ulong second = data[] * * ;
ulong third = data[] * ;
result = first + second + third + data[];
return result;
} public static string ULong2IP(ulong ip)
{
ulong tmp = ip;
ulong last = tmp % ;
tmp = tmp - last;
ulong third = tmp % ( * )/;
tmp = tmp - third;
ulong second = tmp % ( * * )//;
tmp = tmp - third;
ulong first = tmp % ((ulong) * * * )///; return $"{first}.{second}.{third}.{last}";
}

socket操作部分

这里通过绑定指定的端口来实现扫描对等端端口状态

IPAddress ip = IPAddress.Parse(ipStr);
IPEndPoint point = new IPEndPoint(ip, port);Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var result = s.BeginConnect(point, null, null);
result.AsyncWaitHandle.WaitOne(timeoutValue, true);
if (!result.IsCompleted)
{
AddLog($"【{ipStr}:{port}】:timeout");
s.Close();
}
else
{
AddLog($"【{ipStr}:{port}】:success");
s.Close();
}

github  code

布局

布局如下

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902