集册 Java实例教程 当给定的地址是本地地址时返回true。

当给定的地址是本地地址时返回true。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

376
当给定的地址是本地地址时返回true。
/**
时代Java公众号 - N o w J a  v a . c o m 提供 
**/

//package com.nowjava;

import java.net.InetAddress;


public class Main {

    /**

     * Returns true when the given address is a local address.

     * @param addr address

     * @return true when the given address is a local address

     */

    public static boolean hasLocalScope(InetAddress addr) {

        if (addr == null)

            throw new IllegalArgumentException("Must not be null!");

        return (addr.isAnyLocalAddress() || addr.isLinkLocalAddress() || addr

                .isLoopbackAddress());

    }

}