Java Host Address Get getHostAddress()

Here you can find the source of getHostAddress()

Description

get Host Address

License

Apache License

Declaration

public static String getHostAddress() throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class Main {
    public static String getHostAddress() throws Exception {
        final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

        while (interfaces.hasMoreElements()) {
            final NetworkInterface current = interfaces.nextElement();

            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }//from www .ja  va2s .  c  o m

            final Enumeration<InetAddress> addresses = current.getInetAddresses();

            while (addresses.hasMoreElements()) {
                final InetAddress current_addr = addresses.nextElement();

                if (current_addr.isLoopbackAddress()) {
                    continue;
                }

                if (current_addr instanceof Inet4Address) {
                    return current_addr.getHostAddress();
                }
            }
        }

        throw new RuntimeException("Couldn't deduce host address");
    }
}

Related

  1. getHostAddress()
  2. getHostAddress()
  3. getHostAddress()
  4. getHostAddress()
  5. getHostAddress()
  6. getHostAddress()
  7. getHostAddress(boolean promiscuous)
  8. getHostAddress(final String name)
  9. getHostAddress(String hostname)