Java InetAddress Check isLocalAddress(InetAddress address)

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

Description

Cheke whether the given address is local address or not.

License

MIT License

Parameter

Parameter Description
address A network address.

Return

A result.

Declaration

public static boolean isLocalAddress(InetAddress address) 

Method Source Code

//package com.java2s;
/*/*from  ww w .  j a v a 2s.  com*/
 * Copyright (C) 2015 Nameless Production Committee
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://opensource.org/licenses/mit-license.php
 */

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    /**
     * Cheke whether the given address is local address or not.
     * 
     * @param address A network address.
     * @return A result.
     */
    public static boolean isLocalAddress(InetAddress address) {
        // assert null
        if (address == null) {
            return false;
        }

        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();

                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();

                while (addresses.hasMoreElements()) {
                    InetAddress inetAddress = addresses.nextElement();

                    if (inetAddress.equals(address)) {
                        return true;
                    }
                }
            }
        } catch (SocketException e) {
            // do nothing
        }
        return false;
    }
}

Related

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