c#端口扫描器wpf+socket
2024-08-31 02:49:06
布局如下
<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();
}
布局
布局如下
最新文章
- SDK截图程序(一)
- 第三章 springboot + jedisCluster(转载)
- Java多线程12:ReentrantLock中的方法
- JVM学习笔记:虚拟机的类加载机制
- Android Studio的git功能的使用
- ShopEX 4.8.5.81822 前台Getshell
- sqlite与C++进行连接
- Intellij Idea 创建EJB项目入门(一)
- 要源码的快来啊,价值500的OA商业源码免费送给大家,望大家年底奖金多多......
- IOS中UIButton和UIImageView的区别
- wpf只运行一个实例
- Spring的Resource
- UX2 beta 3正式发布!!
- python 的os.getenv(";PATH";)和os.environ.get(";PATH";)的区别
- spring cloud config svn仓库配置
- Queue 队列的使用
- JavaSE 可变参数的方法重载
- PyQt5--MessageBox
- poj2007极角排序
- hdu 1399(水题)