Java – How to check if an IP address is from a particular network/netmask in Java


I need to determine if given IP address is from some special network in order to authenticate automatically.

Best Solution

Apache Commons Net has that appears to satisfy your needs. It looks like you do something like this:

SubnetInfo subnet = (new SubnetUtils("", "")).getInfo();
boolean test = subnet.isInRange("");

Note, as carson points out, that Apache Commons Net has a bug that prevents it from giving the correct answer in some cases. Carson suggests using the SVN version to avoid this bug.