Java IP Address Get getLinuxIPAddress()

Here you can find the source of getLinuxIPAddress()

Description

get Linux IP Address

License

Open Source License

Declaration

private static String getLinuxIPAddress() 

Method Source Code

//package com.java2s;
/*//from w w w . j  ava  2 s  .  co m
 * Copyright (C) 2015 Software&System Lab. Kangwon National University.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;

import java.util.Enumeration;

public class Main {
    private static String getLinuxIPAddress() {
        String hostAddr = "";

        try {
            Enumeration<NetworkInterface> nienum = NetworkInterface.getNetworkInterfaces();
            while (nienum.hasMoreElements()) {
                NetworkInterface ni = nienum.nextElement();
                Enumeration<InetAddress> kk = ni.getInetAddresses();
                while (kk.hasMoreElements()) {
                    InetAddress inetAddress = kk.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                            && inetAddress.isSiteLocalAddress()) {
                        hostAddr = inetAddress.getHostAddress();
                    }
                }
            }

            if (hostAddr == null || hostAddr.equals("")) {
                hostAddr = "127.0.0.1";
            }

        } catch (SocketException e) {
            System.out.println("start Android get");
            try {
                hostAddr = getAndroidIPAddress();
            } catch (Exception ex) {
                System.out.println("Not Linux and Android");
            }

        }
        String retaddr;
        if (hostAddr != null && !hostAddr.equals("")) {
            retaddr = hostAddr;
        } else
            retaddr = "127.0.0.1";
        System.out.println("OSUTILL return ip is " + retaddr);
        return retaddr;
    }

    private static String getAndroidIPAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    System.out.println("ip1--:" + inetAddress);
                    System.out.println("ip2--:" + inetAddress.getHostAddress());

                    // for getting IPV4 format
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                        String ip = inetAddress.getHostAddress().toString();
                        System.out.println("ip---::" + ip);
                        return ip;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. getIPV4NetwprkOrder(String theIp)
  2. getIPV6AddrArray(String s)
  3. getIPV6AllAddresses(NetworkInterface ni)
  4. getIpv6List()
  5. getLanIPAddress()
  6. getLocalAddresses(boolean ipv4only)
  7. getLocalHostIp()
  8. getLocalHostIp()
  9. getLocalHostIP()