.NET 有 IP 地址范围的概念吗?
我需要测试给定的 IP 地址是否在地址范围内。
我可以写一些 API 来给我类似的东西
IPRange ipRange = IPRange.Parse("127.0.0.1-127.0.0.15");
ipRange.Contains(someAddress);
Run Code Online (Sandbox Code Playgroud)
但如果已经内置了类似的功能,我不想重新发明轮子。
不,但这是如何做到的(VB,因为代码标签不包含在OP中)
'test values
Dim rangeStart As Net.IPAddress = Net.IPAddress.Parse("192.168.133.1")
Dim rangeEnd As Net.IPAddress = Net.IPAddress.Parse("192.168.133.254")
Dim check As Net.IPAddress = Net.IPAddress.Parse("192.168.133.230")
'get the bytes of the address
Dim rbs() As Byte = rangeStart.GetAddressBytes
Dim rbe() As Byte = rangeEnd.GetAddressBytes
Dim cb() As Byte = check.GetAddressBytes
'reverse them for conversion
Array.Reverse(rbs)
Array.Reverse(rbe)
Array.Reverse(cb)
'convert them
Dim rs As UInt32 = BitConverter.ToUInt32(rbs, 0)
Dim re As UInt32 = BitConverter.ToUInt32(rbe, 0)
Dim chk As UInt32 = BitConverter.ToUInt32(cb, 0)
'check
If chk >= rs AndAlso chk <= re Then
Debug.WriteLine("In Range")
Else
Debug.WriteLine("Not In Range")
End If
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4502 次 |
| 最近记录: |