Here you can find the source of isLocalAddress(InetAddress address)
Parameter | Description |
---|---|
address | A network address. |
public static boolean isLocalAddress(InetAddress address)
//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; } }