集册 Java实例教程 返回一个InetAddress对象,该对象封装了最有可能是计算机的LAN IP地址。

返回一个InetAddress对象,该对象封装了最有可能是计算机的LAN IP地址。

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

690
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回一个InetAddress对象,该对象封装了最有可能是计算机的LAN IP地址。


//package com.nowjava;

import java.net.InetAddress;

import java.net.NetworkInterface;/*时代Java公众号 提 供*/

import java.net.UnknownHostException;

import java.util.Enumeration;


public class Main {

    public static void main(String[] argv) throws Exception {

        System.out.println(getLocalHostLANAddress());

    }


    /**

     * Returns an <code>InetAddress</code> object encapsulating what is most likely the machine's LAN IP address.

     * <p/>

     * This method is intended for use as a replacement of JDK method <code>InetAddress.getLocalHost</code>, because

     * that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same

     * way as regular LAN network interfaces, but the JDK <code>InetAddress.getLocalHost</code> method does not

     * specify the algorithm used to select the address returned under such circumstances, and will often return the

     * loopback address, which is not valid for network communication. Details

     * <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037">here</a>.

     * <p/>

     * This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address

     * most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer

     * a site-local IP address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the

     * first site-local address if the machine has more than one), but if the machine does not hold a site-local

     * address, this method will return simply the first non-loopback address found (IPv4 or IPv6).

     * <p/>

     * If this method cannot find a non-loopback address using this selection algorithm, it will fall back to

     * calling and returning the result of JDK method <code>InetAddress.getLocalHost</code>.

     * <p/>

     *

     * @throws UnknownHostException If the LAN address of the machine cannot be found.

     */

    public static InetAddress getLocalHostLANAddress()

            throws UnknownHostException {

        try {

            InetAddress candidateAddress = null;

            // Iterate all NICs (network interface cards)...

            for (Enumeration<NetworkInterface> ifaces = NetworkInterface

                    .getNetworkInterfaces(); ifaces.hasMoreElements();) {

                NetworkInterface iface = ifaces.nextElement();

                // Iterate all IP addresses assigned to each card.../** 来 自 n o w j a v a . c o m - 时代Java**/

                for (Enumeration<InetAddress> inetAddrs = iface

                        .getInetAddresses(); inetAddrs.hasMoreElements();) {

                    InetAddress inetAddr = inetAddrs.nextElement();

                    if (!inetAddr.isLoopbackAddress()) {


                        if (inetAddr.isSiteLocalAddress()) {

                            // Found non-loopback site-local address. Return it immediately...

                            return inetAddr;

                        } else if (candidateAddress == null) {

                            // Found non-loopback address, but not necessarily site-local.

                            // Store it as a candidate to be returned if site-local address is not subsequently found...

                            candidateAddress = inetAddr;

                            // Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,

                            // only the first. For subsequent iterations, candidate will be non-null.

                        }

                    }

                }

            }

            if (candidateAddress != null) {

                // We did not find a site-local address, but we found some other non-loopback address.

                // Server might have a non-site-local address assigned to its NIC (or it might be running

                // IPv6 which deprecates the "site-local" concept).

                // Return this non-loopback candidate address...

                return candidateAddress;

            }

            // At this point, we did not find a non-loopback address.

            
展开阅读全文