Java Utililty Methods InetAddress Check

List of utility methods to do InetAddress Check

Description

The list of methods to do InetAddress Check are organized into topic(s).

Method

booleanisThisMyIpAddress(InetAddress addr)
is This My Ip Address
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress())
    return true;
try {
    return NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
    return false;
booleanisUDPPortFree(int port, InetAddress addr)
is UDP Port Free
DatagramSocket ds = null;
try {
    ds = new DatagramSocket(port, addr);
    return true;
} catch (IOException io) {
} finally {
    if (ds != null) {
        ds.close();
...
booleanisUnicastAddress(@CheckForNull InetAddress address)
Determines whether the given address is a "useful" unicast address.
return (address != null) && !address.isAnyLocalAddress() && !address.isLoopbackAddress()
        && !address.isMulticastAddress();
booleanisValidAddress(InetAddress address, int timeoutMs)
Tests if the address is externally resolvable.
return !address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isLoopbackAddress()
        && address.isReachable(timeoutMs) && (address instanceof Inet4Address);
booleanisValidAddress(InetAddress i, boolean includeLocalAddressesInNoderefs)
is Valid Address
if (i.isAnyLocalAddress()) {
    return false;
} else if (i.isLinkLocalAddress() || i.isLoopbackAddress() || i.isSiteLocalAddress()) {
    if (includeLocalAddressesInNoderefs) {
        return true;
    } else
        return false;
} else if (i.isMulticastAddress()) {
...
booleanisValidAddress(InetAddress i, boolean includeLocalAddressesInNoderefs)
is Valid Address
if (i.isAnyLocalAddress()) {
    return false;
} else if (i.isLinkLocalAddress() || i.isLoopbackAddress() || isSiteLocalAddress(i)) {
    if (includeLocalAddressesInNoderefs) {
        return true;
    } else
        return false;
} else if (i.isMulticastAddress()) {
...
booleanisValidCiscoWildcard(InetAddress wildcard)
Determines if a specified address is a valid Cisco Wildcard (cisco's representation of a netmask)
byte[] segments = wildcard.getAddress();
for (int i = 0; i < segments.length; i++) {
    assert (((byte) ~(byte) ~segments[i]) == segments[i]);
    segments[i] = (byte) ~segments[i];
return isValidNetmask(segments);
booleanisValidInetAddress(String address)
is Valid Inet Address
try {
    InetAddress.getByName(address);
    return true;
} catch (UnknownHostException e) {
    return false;
voidisValidInetAddress(String ip)
is Valid Inet Address
if (ip != null && !ip.isEmpty()) {
    if (!ip.contains(":")) {
        String[] segments = ip.split("\\.");
        if (segments.length != 4) {
            throw new IllegalArgumentException("IP does not appear valid:" + ip);
        try {
            InetAddress.getByName(ip);
...
voidisValidInetAddress(String IP)
Verify if IP string is an IPv4 address.
if (IP == null || IP.isEmpty()) {
    throw new IllegalArgumentException("IP is missing or empty");
if (IP.contains(":")) {
} else {
    String[] segments = IP.split("\\.");
    if (segments.length != 4) {
        throw new IllegalArgumentException("IP does not appear valid:" + IP);
...