/**
* 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) {
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。