Java IP Address Get getIp()

Here you can find the source of getIp()

Description

get Ip

License

Open Source License

Declaration

public static String getIp() 

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 getIp() {
        String ip = null;// w ww. j  av a  2s  . c om
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // Filtrando localhost y tarjetas deshabilitadas
                if (iface.isLoopback() || !iface.isUp())
                    continue;

                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    ip = addr.getHostAddress();
                    return ip;
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        return ip;
    }
}

Related

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