Java IP Address Get getIpAddress()

Here you can find the source of getIpAddress()

Description

get Ip Address

License

Open Source License

Declaration

public static String getIpAddress() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.Enumeration;

public class Main {
    public static String getIpAddress() {
        String hostaddress = "localhost";
        try {/*from  w w  w . j a  va 2  s. co  m*/
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();

            while (e.hasMoreElements()) {
                NetworkInterface n = e.nextElement();
                Enumeration<InetAddress> ee = n.getInetAddresses();
                while (ee.hasMoreElements()) {
                    InetAddress i = ee.nextElement();
                    String[] ha = i.getHostAddress().split("\\.");

                    if (ha.length == 4 && !ha[0].equals("127")) {
                        hostaddress = i.getHostAddress();
                    }
                }
            }

        } catch (Exception ex) {
            // TODO: handle exception
        }
        return hostaddress;

    }
}

Related

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