Java InetAddress Create getInetAddress()

Here you can find the source of getInetAddress()

Description

Return the local host address

License

Apache License

Exception

Parameter Description
UnknownHostException if no IP address for the local host could be found.

Return

The first non-loopback address. If there are no non-loopback addresses, return the default host address obtained from .

Declaration

public static InetAddress getInetAddress() throws UnknownHostException 

Method Source Code


//package com.java2s;
/*/* ww  w.j a  v a 2 s . c  om*/
 * Copyright to the original author or authors
 *
 * 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.
 */

import java.net.*;
import java.util.Enumeration;

public class Main {
    /**
     * Return the local host address
     *
     * @return The first non-loopback address. If there are no non-loopback addresses,
     * return the default host address obtained from {@link java.net.InetAddress#getLocalHost()}.
     *
     * @throws UnknownHostException if no IP address for the local host could be found.
     */
    public static InetAddress getInetAddress() throws UnknownHostException {
        InetAddress address = null;
        try {
            address = getFirstNonLoopbackAddress(false, false);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        if (address == null) {
            address = InetAddress.getLocalHost();
        }
        return address;
    }

    /**
     * Get the first non-loopback address.
     *
     * @param preferIpv4 If you want the Internet Protocol version 4 (IPv4) address, this value must be {@code true}.
     * @param preferIPv6 If you want the  Internet Protocol version 6 (IPv6) address, this value must be {@code true}.
     *                   If the {@code preferIpv4} is set to {@code true}, this value is ignored
     * @return The first non loopback address or {@code null} if there are no non-loopback addresses.
     * @throws SocketException If an I/O error occurs.
     */
    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) {
                            continue;
                        }
                        if (preferIpv4)
                            return addr;
                    }
                    if (addr instanceof Inet6Address) {
                        if (preferIpv4) {
                            continue;
                        }
                        if (preferIPv6)
                            return addr;
                    } else
                        return addr;
                }
            }
        }
        return null;
    }
}

Related

  1. getHostName(InetAddress ia)
  2. getHostNameReliably(final String requestingHost, final InetAddress site, final URL requestingUrl)
  3. getHostNameWithoutDomain(final InetAddress addr)
  4. getInetAddress()
  5. getInetAddress()
  6. getInetAddress()
  7. getInetAddress(final int[] octets, final int offset, final int length)
  8. getInetAddress(final String addressOrName)
  9. getInetAddress(long ip)