集册 Java实例教程 检索第一个非

检索第一个非

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

482
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
检索正在运行的计算机的第一个非环回IP地址(即,过滤出127.0.0.1地址)。

/**

 * Copyright (c) Codice Foundation

 * 

 * This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser

 * General Public License as published by the Free Software Foundation, either version 3 of the

 * License, or any later version.

 * 

 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without

 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

 * Lesser General Public License for more details. A copy of the GNU Lesser General Public License

 * is distributed along with this program and can be found at

 * <http://www.gnu.org/licenses/lgpl.html>.

 * 

 **/

//package com.nowjava;
/**
 * 时 代 J     a    v  a - nowjava.com 提 供 
**/

import java.net.Inet4Address;

import java.net.Inet6Address;

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.util.Enumeration;


public class Main {

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

        System.out.println(getFirstNonLoopbackAddress());

    }/** 来 自 N o w J a v a . c o m**/


    /**

     * Retrieves the first non-loopback IP address, (i.e., filters out 127.0.0.1 addresses) for

     * machine your are running on.

     * 

     * @return

     * @throws SocketException

     */

    public static InetAddress getFirstNonLoopbackAddress()

            throws SocketException {

        return getFirstNonLoopbackAddress(true, false);

    }


    /**

     * @param preferIpv4

     * @param preferIPv6

     * @return

     * @throws SocketException

     */

    public static InetAddress getFirstNonLoopbackAddress(

            boolean preferIpv4, boolean preferIPv6) throws SocketException {

        Enumeration en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {

            NetworkInterface i = (NetworkInterface) en.nextElement();

            for (Enumeration en2 = i.getInetAddresses(); en2

                    .hasMoreElements();) {

                InetAddress addr = (InetAddress) en2.nextElement();

                if (!addr.isLoopbackAddress()) {

                    if (addr instanceof Inet4Address) {

                        if (preferIPv6) {

                            
展开阅读全文