Question : vb.net count ip range

hello there,
I would like to count the ranges of ip for example

127.0.0.1-127.0.0.20

to give me total.. how can I do that?

Answer : vb.net count ip range

Here is how I do it...   I conver the "quad dot" notation into a 64-bit long integer.   After that, it's pretty easy to just substract the two numbers to see how many addresses there are between the beginning and ending IP address range:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
    ' VB.Net doesn't support a Union type, but it does allow you to 
    ' "decorate" a structure to accomplish the same thing
    <StructLayout(LayoutKind.Explicit)> _
    Private Structure IP_Parts
        <FieldOffset(0)> Dim address As Int64
        <FieldOffset(3)> Dim Right As Byte
        <FieldOffset(2)> Dim middleRight As Byte
        <FieldOffset(1)> Dim middleLeft As Byte
        <FieldOffset(0)> Dim Left As Byte
    End Structure

    Private Shared Function IPStrToLong(ByVal IPstr As String) As Long
        Dim ip As IPAddress
        Dim parts As IP_Parts
        Dim buf() As String

        ip = [IPAddress].None
        If Not [IPAddress].TryParse(IPstr, ip) Then
            Return 0
        End If

        ' The IPAddress.Address property has been "depreciated", but we still
        ' need a way to convert a IPv4 "quad dot" to and from a long, so just
        ' keep the compiler happy, we use this routine

        buf = ip.ToString.Split("."c)
        If buf.Length <> 4 Then
            ' Sorry, we don't support IPv6 notation
            Return 0
        End If

        parts.Right = CByte(buf(3))
        parts.middleRight = CByte(buf(2))
        parts.middleLeft = CByte(buf(1))
        parts.Left = CByte(buf(0))

        Return parts.address
    End Function
Random Solutions  
 
programming4us programming4us