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