Is there a specific data type for storing IP addresses in Java? I have one specific functionality requirement:
- Given an IP range and an IP, return true if it falls within it, false otherwise.
For example: range 10.10.10.1-10.10.11.255 and IP 10.10.10.192 should return true.
I know of java.net.inetaddress but I believe it doesn’t give me this functionality. Any ideas?
An IP (IPv4) is 32 bits (the same size as an int in Java). Since you want to do comparisons using unsigned ints (if you need to support IP’s above 128.0.0.0) you need to use longs instead.
Since
168430081 <= 168430272 && 168430272 <= 168430591, (In other words 168430272 is between 168430081 and 168430272) your IP is in the range.