Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

In this page you can find the example usage for java.net InetAddress getHostAddress.

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:architecture.ee.web.util.ServletUtils.java

public static String getLocalHostAddr() {
    InetAddress addr = getLocalHost();
    if (addr == null)
        return null;
    return addr.getHostAddress();
}

From source file:Main.java

public static void getLocalIP() {
    String ipaddress = "";
    try {/*from w w  w  . ja va2 s.c o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
    }
}

From source file:com.air.mobilebrowser.NetworkUtil.java

public static String getIPAddress() {
    boolean useIPv4 = true;
    try {//from  w ww .jav a  2s  . co m
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions

    return "";
}

From source file:Main.java

public static String getGPRS_IP() {
    try {//from www .j  a va 2s .  c  o m
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enu = intf.getInetAddresses(); enu.hasMoreElements();) {
                InetAddress inetAddress = enu.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                    if (inetAddress.getHostAddress().toString().contains("::")) {
                        continue;
                    }
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;

}

From source file:eu.codebits.plasmas.util.NetworkInterfaces.java

/**
 * @param interfaceName eth0, wlan0 or NULL=use first interface 
 * @param useIPv4//from  w  w w  .  jav a2 s .c o  m
 * @return address or empty string
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress(String interfaceName, boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (SocketException ex) {
    }
    return "";
}

From source file:org.nebula.service.core.HostAddress.java

public static String getLocalHost() throws Exception {

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface current = interfaces.nextElement();

        if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
            continue;
        }//  w  ww . j  ava2  s  .  c om
        Enumeration<InetAddress> addresses = current.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress current_addr = addresses.nextElement();
            if (current_addr.isLoopbackAddress()
                    || !InetAddressUtils.isIPv4Address(current_addr.getHostAddress())) {
                continue;
            }
            return current_addr.getHostName();
        }
    }

    throw new Exception("Failed to get local hostname");
}

From source file:gemlite.core.internal.admin.AdminUtil.java

/**
 * ?ip//  w  w  w. jav  a 2  s  . c  o  m
 * 
 * @return
 */
public static String getIp() {
    String ip = System.getProperty(ITEMS.BINDIP.name());
    if (StringUtils.isEmpty(ip)) {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress().toString();
            ip += IPSUFFIX; // ???
        } catch (Exception e) {
            ip = e.getMessage();
            LogUtil.getAppLog().error("get Ip error:", e);
        }
    }
    return ip;
}

From source file:Main.java

public static String getIPAdd() {
    try {//from   w w w  .  j  a va 2  s  .  c  om
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.github.christiangda.utils.ip.IP.java

/**
 * @param ip InetAddress//from   ww w.  ja va  2  s . c o  m
 * @return boolean
 */
public static boolean isValidIPV6(InetAddress ip) {
    if (ip == null) {
        return false;
    }
    return InetAddressUtils.isIPv6Address(ip.getHostAddress());
}

From source file:com.clustercontrol.repository.util.NodeSearchUtil.java

/**
 * Generate default IP for node search/create dialog
 *//*from  ww  w  .j ava 2  s  . c o  m*/
public static String generateDefaultIp(String def) {
    try {
        InetAddress addr = Inet4Address.getLocalHost();
        // NOTE: Only correct when netmask length is 24
        return addr.getHostAddress().replaceFirst("\\.\\d+$", ".");
    } catch (UnknownHostException e) {
        m_log.debug(e);
    }
    return def;
}