布局如下

<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

布局

布局如下

最新文章

  1. SDK截图程序(一)
  2. 第三章 springboot + jedisCluster(转载)
  3. Java多线程12:ReentrantLock中的方法
  4. JVM学习笔记:虚拟机的类加载机制
  5. Android Studio的git功能的使用
  6. ShopEX 4.8.5.81822 前台Getshell
  7. sqlite与C++进行连接
  8. Intellij Idea 创建EJB项目入门(一)
  9. 要源码的快来啊,价值500的OA商业源码免费送给大家,望大家年底奖金多多......
  10. IOS中UIButton和UIImageView的区别
  11. wpf只运行一个实例
  12. Spring的Resource
  13. UX2 beta 3正式发布!!
  14. python 的os.getenv(&quot;PATH&quot;)和os.environ.get(&quot;PATH&quot;)的区别
  15. spring cloud config svn仓库配置
  16. Queue 队列的使用
  17. JavaSE 可变参数的方法重载
  18. PyQt5--MessageBox
  19. poj2007极角排序
  20. hdu 1399(水题)

热门文章

  1. 黑客正在使用美国NSA泄露的工具进行挖矿
  2. Rust 基础学习
  3. linux centos 7安装 apache php 及mariadb
  4. css 浮动的知识点
  5. cmd窗口颜色设置
  6. pmf文件
  7. 《编译原理》求 FIRSTVT 集和 LASTVT 集的步骤 - 例题解析
  8. 【洛谷P2915】Mixed Up Cows
  9. 代码自动补全插件CodeMix全新发布CI 2019.7.15|改进CSS颜色辅助
  10. 使用nfs制作动态分配存储卷