如果我有一个IP地址范围(例如192.168.0.0 - 192.168.0.255)并且我正在计数(比如以随机数递增),我怎么能确定我是否超过了该范围? / p>
答案 0 :(得分:1)
只需将IP地址转换为数字并比较数字即可。
Dim ipmax As Net.IPAddress = Net.IPAddress.Parse("192.168.0.255")
Dim ip1 As Net.IPAddress = Net.IPAddress.Parse("192.168.1.0")
Dim ipmaxAsNum As UInt32 = BitConverter.ToUInt32(ipmax.GetAddressBytes.Reverse.ToArray, 0)
Dim ip1AsNum As UInt32 = BitConverter.ToUInt32(ip1.GetAddressBytes.Reverse.ToArray, 0)
If ip1AsNum > ipmaxAsNum Then
Stop
Else
Stop
End If
答案 1 :(得分:0)
'''<summary>Checks if IP Address A is larger than IP Address B. Returns True if A is larger, returns False if B is larger.</summary>
Private Function Check_If_Larger(ByVal IP_Address_A As Net.IPAddress, ByVal IP_Address_B As Net.IPAddress) As Boolean
Dim IP_Address_A_Split() As String = IP_Address_A.ToString.Split("."c)
Dim IP_Address_B_Split() As String = IP_Address_B.ToString.Split("."c)
Dim IP_Address_A_Score As Integer = 0
Dim IP_Address_B_Score As Integer = 0
For i As Integer = 0 To IP_Address_A_Split.Length - 1
If Convert.ToInt32(IP_Address_A_Split(i)) > Convert.ToInt32(IP_Address_B_Split(i)) Then
IP_Address_A_Score += 1
ElseIf Convert.ToInt32(IP_Address_B_Split(i)) > Convert.ToInt32(IP_Address_A_Split(i)) Then
IP_Address_B_Score += 1
End If
Next
If IP_Address_A_Score > IP_Address_B_Score Then
Return True
Else
Return False
End If
End Function
答案 2 :(得分:0)
我相信ip地址中存在一个值层次结构,从右到左变大。即,最左边的值比最右边的值更重要,并且对ip地址的大小有更大的影响。例如。 192.168.1.0大于192.168.0.255
根据分层结构分配分数会有所帮助。 E.g。
Private Function Max(A As String, B As String) As String
Dim AA() As String = A.Split(".")
Dim BB() As String = B.Split(".")
Dim A_Score As Integer = 0
Dim B_Score As Integer = 0
For i = 0 To AA.Length - 1
If AA(i) > BB(i) Then
A_Score += AA.Length - i
ElseIf BB(i) > AA(i) Then
B_Score += BB.Length - i
End If
Next
If A_Score > B_Score Then
Return A
ElseIf B_Score > A_Score Then
Return B
Else
Return ""
End If
End Function