Java InetAddress Check isLocalAddress(InetAddress address)

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

Description

Checks if the given address is one of the addresses of the current machine.

License

Apache License

Parameter

Parameter Description
address The address to check.

Return

Is it one of the machine addresses.

Declaration

public static boolean isLocalAddress(InetAddress address) 

Method Source Code

//package com.java2s;
/**// ww w.j a va  2 s  .  c  o  m
 * Appia: Group communication and protocol composition framework library
 * Copyright 2006 University of Lisbon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *
 * Initial developer(s): Alexandre Pinto and Hugo Miranda.
 * Contributor(s): See Appia web page for a list of contributors.
 */

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

import java.util.Enumeration;

public class Main {
    /**
     * Checks if the given address is one of the addresses of the current machine.
     * 
     * @param address The address to check.
     * @return Is it one of the machine addresses.
     */
    public static boolean isLocalAddress(InetAddress address) {
        NetworkInterface intf;
        InetAddress addr;
        Enumeration<InetAddress> eips = null;
        Enumeration<NetworkInterface> eintfs = null;

        try {
            eintfs = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e1) {
            e1.printStackTrace();
        }
        while ((eintfs != null) && eintfs.hasMoreElements()) {
            intf = eintfs.nextElement();
            eips = intf.getInetAddresses();
            while (eips.hasMoreElements()) {
                addr = eips.nextElement();
                if (address.equals(addr))
                    return true;
            }
        }
        return false;
    }
}

Related

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