Java InetAddress Check isLocalAddress(InetAddress addr)

Here you can find the source of isLocalAddress(InetAddress addr)

Description

Given an InetAddress, checks to see if the address is a local address, by comparing the address with all the interfaces on the node.

License

Apache License

Parameter

Parameter Description
addr address to check if it is local node's address

Return

true if the address corresponds to the local node

Declaration

public static boolean isLocalAddress(InetAddress addr) 

Method Source Code

//package com.java2s;
// Licensed to the Apache Software Foundation (ASF) under one

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

public class Main {
    /**/* w  ww . j  av  a 2 s  . co m*/
     * Given an InetAddress, checks to see if the address is a local address, by
     * comparing the address with all the interfaces on the node.
     * @param addr address to check if it is local node's address
     * @return true if the address corresponds to the local node
     */
    public static boolean isLocalAddress(InetAddress addr) {
        // Check if the address is any local or loopback.
        boolean local = addr.isAnyLocalAddress()
                || addr.isLoopbackAddress();

        // Check if the address is defined on any interface.
        if (!local) {
            try {
                local = NetworkInterface.getByInetAddress(addr) != null;
            } catch (SocketException e) {
                // Pass.
            }
        }
        return local;
    }
}

Related

  1. isLikelyBroadcast(InetAddress address)
  2. isLinkLocalIPv4Address(InetAddress add)
  3. isLinkLocalNetwork(InetAddress addr)
  4. isLocal(InetAddress a)
  5. isLocalAddress(InetAddress addr)
  6. isLocalAddress(InetAddress addr)
  7. isLocalAddress(InetAddress address)
  8. isLocalAddress(InetAddress address)
  9. isLocalIP(final InetAddress ip1, final InetAddress ip2, final int mask)