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;

import java.net.Inet6Address;
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 = "";
        try {/* w w w.  jav a 2  s . co  m*/
            Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
            while (e1.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) e1.nextElement();
                if (!ni.getName().equals("eth0")) {
                    continue;
                } else {
                    Enumeration<?> e2 = ni.getInetAddresses();
                    while (e2.hasMoreElements()) {
                        InetAddress ia = (InetAddress) e2.nextElement();
                        if (ia instanceof Inet6Address)
                            continue;
                        ip = ia.getHostAddress();
                    }
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return ip;
    }
}

Related

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