Java IP Address Get getIpAddress()

Here you can find the source of getIpAddress()

Description

get Ip Address

License

Apache License

Declaration

public static String getIpAddress() 

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 getIpAddress() {
        String rtip = "unknown.host";
        try {/* www .j av  a 2 s .  c o m*/
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            rtip = ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
        }

        return rtip;
    }
}

Related

  1. getIp()
  2. getIP()
  3. getIp()
  4. getIpAddress()
  5. getIPAddress()
  6. getIpAddress()
  7. getIpAddress()
  8. getIpAddress()
  9. getIpAddress()