Java InetAddress Check isThisMyIpAddress(InetAddress addr)

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

Description

Check if the given address is pointing to this local machine.

License

Open Source License

Parameter

Parameter Description
addr the address to be checked

Return

true if the address is pointing to the local machine, false otherwise

Declaration

public static boolean isThisMyIpAddress(InetAddress addr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/* w  w w  .ja  v  a  2  s  .c  o  m*/
     * Check if the given address is pointing to this local machine.
     * 
     * @param addr
     *            the address to be checked
     * @return true if the address is pointing to the local machine, false
     *         otherwise
     */
    public static boolean isThisMyIpAddress(InetAddress addr) {
        /* check if the address is a valid special local or loop back */
        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
            return true;
        }
        /* check if the address is defined on any interface */
        try {
            return NetworkInterface.getByInetAddress(addr) != null;
        } catch (SocketException ex) {
            return false;
        }
    }
}

Related

  1. isReservedAddr(InetAddress inetAddr)
  2. isServerAlive(InetAddress host, int port)
  3. isServerAlive(InetAddress host, int port)
  4. isSiteLocalAddress(InetAddress i)
  5. isTenDot(InetAddress adr)
  6. isThisMyIpAddress(InetAddress addr)
  7. isUDPPortFree(int port, InetAddress addr)
  8. isUnicastAddress(@CheckForNull InetAddress address)
  9. isValidAddress(InetAddress address, int timeoutMs)