How to check if IP in IP range

xdscheetos

Member
Joined
Nov 11, 2003
Messages
6
Got an answer so fast last time I posted :cool:, I thought I might ask for some more help :
Is there a quick way to check an IP address against a network range.

I want to check 192.168.0.15 against my network (192.168.5.1-254) to see if it is (or not in this case) in my IP range.

I have the IP address of my machine now I just need to know if there is a function to make that check...
myIP = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList(0)

=> is myIP in 192.168.5.1 to 192.168.5.254 :D

Thanks
 
I dont believe there to be any "quick" way. But hows this?

C#:
byte[] local = Dns.Resolve(Dns.GetHostName()).AddressList[0].GetAddressBytes();
byte[] mask = new IPAddress("255.255.255.0").GetAddressBytes();
byte[] net = new IPAddress("192.168.1.0").GetAddressBytes();
bool isequal = true;
for (int i = 0; i < net.Length; i++)
    if ((local[i] & mask[i]) != net[i])
    {
        isequal = false;
        break;
    }
 
Thanks for your answer,
yours seem a bit more inline with network programming but heres how I did mine:

Code:
Dim a() As String
a = Split(b.ToString, ".")
if a(0) = "192" Then
    If a(1) <> "168" Then booIPGood = False
    If a(2) <> "10" Then booIPGood = False
    If Not (0 < Convert.ToInt16(a(3)) < 255) Then booIPGood = False
end if
p.s. I might just redo my code to see how yours works out :)

Thanks
edit:added the [ vb ] tag
 
Back
Top