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

后端 未结 7 2160
北恋
北恋 2020-11-29 23:33

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

相关标签:
7条回答
  • 2020-11-30 00:06

    Apache Commons Net has org.apache.commons.net.util.SubnetUtils that appears to satisfy your needs. It looks like you do something like this:

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

    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.

    0 讨论(0)
提交回复
热议问题