Java Local Address Get getLocalAddressAsString()

Here you can find the source of getLocalAddressAsString()

Description

get Local Address As String

License

Open Source License

Declaration

private static String getLocalAddressAsString() throws UnknownHostException, SocketException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class Main {
    private static String getLocalAddressAsString() throws UnknownHostException, SocketException {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces != null && interfaces.hasMoreElements()) {
            Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
            while (addresses != null && addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (acceptableAddress(address)) {
                    return address.getHostAddress();
                }//w w w  .  j a  v a 2  s  .  c  o  m
            }
        }
        throw new UnknownHostException();
    }

    private static boolean acceptableAddress(InetAddress address) {
        return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress()
                && !address.isLinkLocalAddress();
    }
}

Related

  1. getLocalAddress(int port)
  2. getLocalAddress(int port)
  3. getLocalAddress(String adaptorName)
  4. getLocalAddress(String filter)
  5. getLocalAddress0()
  6. getLocalAddresses()
  7. getLocalAddresses()
  8. getLocalAddresses()
  9. getLocalAddresses()