Click to See Complete Forum and Search --> : question about subnetmask


Waqas_Badar
May 23rd, 2008, 07:23 AM
is there any method in C# to which i pass ip address and subnetmask and it return true if given ip is in subnet mask?

For example, i give ip 192.168.0.149 and subnetmask 192.168.0.0 to the funtion and it return true................... Is there some thing like this in C#

TheCPUWizard
May 23rd, 2008, 07:29 AM
If you have the IP (v4) address stored in a long....

bool match = (addr & mask) == mask;


If you have it in a byte array..

bool match = (addr[0] & mask[0]) == mask[0]) &&
(addr[1] & mask[1]) == mask[1]) &&
(addr[2] & mask[2]) == mask[2]) &&
(addr[3] & mask[3]) == mask[3]);


If you have it in a string.... DONT! Change your design. :p