PC Review


Reply
Thread Tools Rate Thread

Fastest way to Check if an IP is in a given range?

 
 
Russ Ryba
Guest
Posts: n/a
 
      16th Mar 2005
Hi,

I'm wondering what the fastest way to Check if an IP is in a given range?

Right now I convert the address to a string (192.168.X.X type) and then
parse the string to see if a given string is in the range. I know with
the data conversion I'm doing I must be going quite slow.

Is there a way to check using just the System.Net.IPAddress found in an
AddressList without converting to string?

Thanks,
- Russ
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      16th Mar 2005
Each octet is a byte, so you can simply do a numerical comparison on them.
Check byte[2] then byte[3].

-Chris

"Russ Ryba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I'm wondering what the fastest way to Check if an IP is in a given range?
>
> Right now I convert the address to a string (192.168.X.X type) and then
> parse the string to see if a given string is in the range. I know with
> the data conversion I'm doing I must be going quite slow.
>
> Is there a way to check using just the System.Net.IPAddress found in an
> AddressList without converting to string?
>
> Thanks,
> - Russ



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      17th Mar 2005
If you are checking against netmask, you can use
IPAddress addr;

if ( (addr.Address & mask) != 0 )
{
// do something
}

if you need to compare agains a range that cannot be representedby a mask,
then just compare the 1st octet:

int lastOctet = IPAddress.NetworkToHostOrder(addr.Address) & 0xff;
Dim lastOctet as Integer = IPAddress.NetworkToHostOrder(addr.Address) And
&HFF


--
Alex Feinman
---
Visit http://www.opennetcf.org
"Russ Ryba" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I'm wondering what the fastest way to Check if an IP is in a given range?
>
> Right now I convert the address to a string (192.168.X.X type) and then
> parse the string to see if a given string is in the range. I know with
> the data conversion I'm doing I must be going quite slow.
>
> Is there a way to check using just the System.Net.IPAddress found in an
> AddressList without converting to string?
>
> Thanks,
> - Russ


 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      17th Mar 2005
Hey,

I just try to do the same...
I think you schould convert the IP-Range into two Long and then you can
check if the IP is between the two values.

What I'm searching for is an easy way to get out of an IP and the
subnetmask the range of the possible IPs like:
192.168.0.1 Subnet 255.255.240.0

Chris



Public Function IP2Long(ByVal DottedIP As String) As Object
Dim arr = Split(DottedIP, ".")
If UBound(arr) = 3 Then
For i As Byte = 1 To 4
If arr(i - 1) > 255 Or arr(i - 1) < 0 Then Return 0
IP2Long = ((arr(i - 1) Mod 256) * (256 ^ (4 - i))) + IP2Long
Next
End If
End Function

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The fastest way to count values in a range avi Microsoft Excel Programming 3 24th Sep 2007 11:46 PM
Fastest way to select large range (e.g. B3:F1002)? nomail1983@hotmail.com Microsoft Excel Misc 7 31st Aug 2007 05:36 PM
What is the fastest way to copy a range to a 2D array? equiangular Microsoft Excel Programming 10 12th Feb 2007 03:25 PM
Fastest way to check if a record exists in SQLCE using ADO.NET =?Utf-8?B?U2ltb24gSGFydA==?= Microsoft Dot NET Compact Framework 0 2nd Nov 2005 03:09 PM
fastest way to check if a table contains the data miro.cepciansky@va-utveckling.se Microsoft ADO .NET 2 13th Nov 2003 12:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:12 PM.