?
Solved

Get ip addresses from a given range (192.168.2.1-192.168.2.255)

Posted on 2009-12-21
6
Medium Priority
?
473 Views
Last Modified: 2013-11-26
First off, let me say that I HAVE been looking for an example of how to do this, but surprisingly it is not a very popular topic\subject.

In VB.Net:

I need to get a list of IP addresses from a given range (192.168.2.1-192.168.2.255)

A VERY VAGUE EXAMPLE:

dim minimum_val as integer = textbox1.text
dim max_value as integer = textbox2.text

???
listbox1.items.add(current_ip)
???
0
Comment
Question by:APag96
  • 4
  • 2
6 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 200 total points
ID: 26105219
Here is what I use:

Sample usage:

        Dim ipAddressRange As List(Of String) = IpAddressHelper.GetAddressRange("192.168.1.1", "192.168.1.100")

Public Class IpAddressHelper

    Public Shared Function GetAddressRange(ByVal startAddress As String, ByVal endAddress As String) As List(Of String)
        Dim list As New List(Of String)

        Dim n1 As Long = IpAddressToNumber(startAddress)
        Dim n2 As Long = IpAddressToNumber(endAddress)

        For i As Long = n1 To n2
            list.Add(NumberToIpAddress(i))
        Next i
        Return list
    End Function

    Public Shared Function IpAddressToNumber(ByVal ipAddress As String) As Long
        Dim parts() As String = ipAddress.Split(".")
        Dim sum As Long
        If parts.Length <> 4 Then
            Throw New ArgumentException("Invalid IP address:  " & ipAddress)
        End If

        For i As Integer = parts.Length - 1 To 0 Step -1
            sum += (Int(parts(i)) Mod 256) * Math.Pow(256, 3 - i)
        Next i
        Return sum
    End Function

    Public Shared Function NumberToIpAddress(ByVal ipAddressNumber As Long) As String
        Dim sb As New StringBuilder()
        For i As Integer = 1 To 4
            Dim factor As Integer = (256 ^ (4 - i))
            Dim number As Long = ipAddressNumber \ factor
            ipAddressNumber -= number * factor
            sb.Append(number & ".")
        Next i
        Return sb.ToString().TrimEnd(".")
    End Function

End Class

Open in new window

0
 

Author Comment

by:APag96
ID: 26108327
Looks good. I'll try this out and get back to you. Thanks.
0
 

Author Comment

by:APag96
ID: 26108637
Thank you very much! That is exactily what I was looking for. 50 points for you!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Closing Comment

by:APag96
ID: 31668790
Perfect.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 26108676
"I don't need no stinkin' points"  The "Thank you very much!" means more to me.
0
 

Author Comment

by:APag96
ID: 26114785
:) You deserve the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question