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.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    public static String getIP() {
        String ipAddress = null;/*from   www .  j a va 2s .  c o m*/
        Enumeration<NetworkInterface> net = null;
        try {
            net = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }

        while (net.hasMoreElements()) {
            NetworkInterface element = net.nextElement();
            Enumeration<InetAddress> addresses = element.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress ip = addresses.nextElement();
                if (ip instanceof Inet4Address) {

                    if (ip.isSiteLocalAddress()) {

                        ipAddress = ip.getHostAddress();
                    }
                }
            }
        }
        return ipAddress;
    }
}

Related

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