Comparing IP Addresses in .Net 2.0

I’ve been working with the new ping class in a lot of my utilities lately since .Net
2.0 has made it so much easier to ping. However, today I ran across a problem when
I wanted to sort a listview based on the ip address column. I found an older example
on the web for sorting in .Net 1.0 that implemented a custom ListViewItem comparer.
It was based on converting the listviewitem text to an IPAddress, then calling the
IPAddress.Address.CompareTo method like this:

   Dim firstIP As IPAddress = IPAddress.Parse(li1.SubItems(1).Text.ToString)
   Dim secondIP As IPAddress = IPAddress.Parse(li2.SubItems(1).Text.ToString)
   Return firstIP.Address.CompareTo(secondIP.address)

When
I did this I got an intellisense error:

Warning 13 ’Public
Property Address() As Long’ is obsolete: ‘This property has been deprecated. It is
address family dependent. Please use IPAddress.Equals method to perform comparisons. http://go.microsoft.com/fwlink/?linkid=14202′ 

So
apparently I can’t use compareTo anymore, and equals is no good for a ListViewItem
comparer since it only returns a boolean value. I decided instead that I should just
convert each IP Address to a long integer and compare that way. I do not deal in bytes
and octets a lot, so if someone sees an error in the way I handle this, please let
me know. Here is the on_click event for the listview and the ListViewItem Comparer
and the function I used to convert the IP Address.

Private Sub resultsListView_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles resultsListView.ColumnClick
   ’
Set the ListViewItemSorter property to a new ListViewItemComparer object.

   Me.resultsListView.ListViewItemSorter = New ListViewItemComparer(e.Column)
   ’ 
Call the sort method to manually sort the column based on the ListViewItemComparer
implementation.


   resultsListView.Sort()
End Sub


Implements the manual sorting of items by columns.


Class ListViewItemComparer
   Implements IComparer

   Private col As Integer

   Public Sub New()
      col = 0
   End Sub

   Public Sub New(ByVal column As Integer)
      col = column
   End Sub

   Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _   
      Implements IComparer.Compare

      If col = 0 Or col = 2 Then

         ’
In my implementation, columns 0 and 2 are just string columns so no special handling
here


         Return [String].Compare(CType(x,
ListViewItem).SubItems(col).Text, CType(y,
ListViewItem).SubItems(col).Text)
      ElseIf col = 1 Then

         ’
This is the part that deals with the IP Address


         Dim li1 As ListViewItem = CType(x,
ListViewItem)
         Dim li2 As ListViewItem = CType(y,
ListViewItem)
         ’
Parse the text from the column as an IP Address


         Dim firstIP As IPAddress = IPAddress.Parse(li1.SubItems(1).Text.ToString)
         Dim secondIP As IPAddress = IPAddress.Parse(li2.SubItems(1).Text.ToString)
         ’
Convert both IPAddress variables to a long integer then return the comparison


         Return ConvertToLongIP(firstIP).CompareTo(ConvertToLongIP(secondIP))
      End If

   End Function

   Function ConvertToLongIP(ByVal Ip As IPAddress) As Long

      Dim longIP As Long = 0
      Dim ipArray() As Byte

      ’
Split the IP address using the dot as a delimiter


      ipArray = Ip.GetAddressBytes

      ’
Loop through Each number In the IP address


      For Index As Integer = 0 To 3
         ’
If we are Not working With the last number…


         If Not Index = 3 Then

            ’
Convert the number To a value range that can be parsed from the others


            longIP +=
ipArray(Index) * (256
^ (3 - Index))
         Else

            ’
Add the number To the results


            longIP = longIP + ipArray(Index)
         End If

      Next

      Return longIP
   End Function

End Class

Random Posts

Loading…

Leave a Reply