Java IP Address Get getLocalIPAddress()

Here you can find the source of getLocalIPAddress()

Description

get Local IP Address

License

Open Source License

Declaration

public static String getLocalIPAddress() throws 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.util.Enumeration;

public class Main {
    public static String getLocalIPAddress() throws SocketException {
        String ipAddress = null;//from   www  .j a  v a 2  s .c om
        Enumeration<NetworkInterface> eIf = NetworkInterface.getNetworkInterfaces();
        while (ipAddress == null && eIf.hasMoreElements()) {
            NetworkInterface nIf = eIf.nextElement();
            Enumeration<InetAddress> eAddress = nIf.getInetAddresses();
            while (eAddress.hasMoreElements()) {
                InetAddress address = eAddress.nextElement();
                if (!address.isLoopbackAddress()) {
                    ipAddress = address.getHostAddress();
                }
            }
        }
        return ipAddress;
    }
}

Related

  1. getLocalIP()
  2. getLocalIp()
  3. getLocalIp(final boolean useIpv6)
  4. getLocalIPAddress()
  5. getLocalIPAddress()
  6. getLocalIpAddress()
  7. getLocalIpAddress()
  8. getLocalIPAddresses()
  9. getLocalIpAddresses()