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="450" Width="800">
    <Grid>
        <Menu>
            <MenuItem Header="About" FontSize="15" Name="About" Click="About_Click"/>
        </Menu>
        <StackPanel Margin="20">
            <WrapPanel>
                <Label FontSize="15" >开始ip</Label>
                <TextBox Width="300" Name="startIP"  FontSize="15" VerticalAlignment="Center"></TextBox>
                <Label FontSize="15">开始端口</Label>
                <TextBox Width="300" Name="startPort" PreviewTextInput="Number_PreviewTextInput"  FontSize="15" VerticalAlignment="Center"></TextBox>
            </WrapPanel>
            <WrapPanel Margin="0 10">
                <Label FontSize="15">结束ip</Label>
                <TextBox Width="300" Name="endIP" GotFocus="endIP_GotFocus"  FontSize="15" VerticalAlignment="Center"></TextBox>
                <Label FontSize="15">结束端口</Label>
                <TextBox Width="300" Name="endPort" PreviewTextInput="Number_PreviewTextInput" GotFocus="endPort_GotFocus"  FontSize="15" VerticalAlignment="Center"></TextBox>
            </WrapPanel>
            <WrapPanel>
                <Label FontSize="15">超时时长(ms)</Label>
                <TextBox Name="timeOut" PreviewTextInput="Number_PreviewTextInput" FontSize="15" VerticalAlignment="Center" Width="252">2000</TextBox>
            </WrapPanel>
            <Button Click="Btn_Test" Width="200" Height="54" Margin="276,15">开始测试</Button>
            <ListView Name="resultListView" Height="184"></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 = 0;
            ulong first = data[0] * 256 * 256 * 256;
            ulong second = data[1] * 256 * 256;
            ulong third = data[2] * 256;
            result = first + second + third + data[3];
            return result;
        }

        public static string ULong2IP(ulong ip)
        {
            ulong tmp = ip;
            ulong last = tmp % 256;
            tmp = tmp - last;
            ulong third = tmp % (256 * 256)/256;
            tmp = tmp - third;
            ulong second = tmp % (256 * 256 * 256)/256/256;
            tmp = tmp - third;
            ulong first = tmp % ((ulong)256 * 256 * 256 * 256)/256/256/256;

            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

布局

布局如下

猜你喜欢

转载自www.cnblogs.com/ives/p/10876886.html