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:com.triggertrap.ZeroConf.java

/**
 * Returns the first found IP4 address.//  ww w  .j a va 2s  .  com
 * 
 * @return the first found IP4 address
 */
public static InetAddress getIPAddress(int wifi_ipaddr) {
    try {
        String ipString = String.format("%d.%d.%d.%d", (wifi_ipaddr & 0xff), (wifi_ipaddr >> 8 & 0xff),
                (wifi_ipaddr >> 16 & 0xff), (wifi_ipaddr >> 24 & 0xff));

        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() && (ipString.equals(addr.getHostAddress()))) {
                    //String sAddr = addr.getHostAddress().toUpperCase();
                    if (addr instanceof java.net.Inet4Address) {
                        //Log.d("found IP address to listen: " , sAddr);
                        return addr;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.synapse.transport.passthru.util.PassThroughTransportUtils.java

/**
 * Whatever this method returns as the IP is ignored by the actual http/s listener when
 * its getServiceEPR is invoked. This was originally copied from axis2
 *
 * @return Returns String.//  w  w  w  .j a  v  a 2s . com
 * @throws java.net.SocketException if the socket can not be accessed
 */
public static String getIpAddress() throws SocketException {
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    String address = "127.0.0.1";

    while (e.hasMoreElements()) {
        NetworkInterface netface = (NetworkInterface) e.nextElement();
        Enumeration addresses = netface.getInetAddresses();

        while (addresses.hasMoreElements()) {
            InetAddress ip = (InetAddress) addresses.nextElement();
            if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
                return ip.getHostAddress();
            }
        }
    }
    return address;
}

From source file:cycronix.CTandroid.CTAserver.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*from  www. j  a  v a2 s .  c o m*/
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        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();
                    // org.apache.http.conn.util is no longer supported under Android API 23
                    // http://stackoverflow.com/questions/32141785/android-api-23-inetaddressutils-replacement
                    // https://developer.android.com/sdk/api_diff/23/changes.html
                    // boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    InetAddress tempInetAddress = InetAddress.getByName(sAddr);
                    boolean isIPv4 = false;
                    if (tempInetAddress instanceof Inet4Address) {
                        isIPv4 = true;
                    }
                    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 "Unknown";
}

From source file:com.ephesoft.dcma.util.NetworkUtil.java

/**
 * Utility method which will fetch the ip address of the system.
 * //from   www  . jav  a  2s . com
 * @return ip address of the system.
 */
public static String getSystemIPAddress() {

    StringBuilder input = new StringBuilder(UtilConstants.INPUT_CONST);
    boolean first = true;
    String returnAddress = null;
    try {
        for (Enumeration<NetworkInterface> enumer = NetworkInterface.getNetworkInterfaces(); enumer
                .hasMoreElements();) {
            NetworkInterface netInterface = enumer.nextElement();
            Enumeration<InetAddress> inetEnum = netInterface.getInetAddresses();
            while (inetEnum.hasMoreElements()) {
                InetAddress inetAddress = inetEnum.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    if (first) {
                        first = false;
                    } else {
                        input.append(UtilConstants.FORWARD_SLASH);
                    }
                    input.append(inetAddress.getHostAddress());
                }
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        returnAddress = IPADDRESS;
    }
    if (null == returnAddress) {
        returnAddress = first ? IPADDRESS : input.toString();
    }
    return returnAddress;
}

From source file:NetworkUtil.java

/**
 * @return the current environment's IP address, taking into account the Internet connection to any of the available
 *         machine's Network interfaces. Examples of the outputs can be in octats or in IPV6 format.
 * <pre>//from w  ww . ja  v  a2  s.co m
 *         ==> wlan0
 *         
 *         fec0:0:0:9:213:e8ff:fef1:b717%4 
 *         siteLocal: true 
 *         isLoopback: false isIPV6: true
 *         130.212.150.216 <<<<<<<<<<<------------- This is the one we want to grab so that we can. 
 *         siteLocal: false                          address the DSP on the network. 
 *         isLoopback: false 
 *         isIPV6: false 
 *         
 *         ==> lo 
 *         0:0:0:0:0:0:0:1%1 
 *         siteLocal: false 
 *         isLoopback: true 
 *         isIPV6: true 
 *         127.0.0.1 
 *         siteLocal: false 
 *         isLoopback: true 
 *         isIPV6: false
 *  </pre>
 */
public static String getCurrentEnvironmentNetworkIp() {
    if (currentHostIpAddress == null) {
        Enumeration<NetworkInterface> netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();

            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> address = ni.getInetAddresses();
                while (address.hasMoreElements()) {
                    InetAddress addr = address.nextElement();
                    //                      log.debug("Inetaddress:" + addr.getHostAddress() + " loop? " + addr.isLoopbackAddress() + " local? "
                    //                            + addr.isSiteLocalAddress());
                    if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()
                            && !(addr.getHostAddress().indexOf(":") > -1)) {
                        currentHostIpAddress = addr.getHostAddress();
                    }
                }
            }
            if (currentHostIpAddress == null) {
                currentHostIpAddress = "127.0.0.1";
            }

        } catch (SocketException e) {
            //                log.error("Somehow we have a socket error acquiring the host IP... Using loopback instead...");
            currentHostIpAddress = "127.0.0.1";
        }
    }
    return currentHostIpAddress;
}

From source file:edu.stanford.epad.epadws.processing.pipeline.task.EpadStatisticsTask.java

public static String getIPAddress() {
    String ip = "";
    String ipi = "";
    Enumeration e;/*from   ww  w  . j a va2  s. c o  m*/
    try {
        e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                ipi = i.getHostAddress();
                if (!ipi.startsWith("127") && !ipi.startsWith("192") && !ipi.startsWith("172")
                        && !ipi.startsWith("10.") && !ipi.startsWith("0:"))
                    ip = ipi;
            }
        }
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    if (ip.length() == 0)
        return ipi;
    else
        return ip;
}

From source file:com.ery.ertc.estorm.util.DNS.java

/**
 * Returns the hostname associated with the specified IP address by the provided nameserver.
 * // ww w  .  ja v a2s .  co  m
 * @param hostIp
 *            The address to reverse lookup
 * @param ns
 *            The host name of a reachable DNS server
 * @return The host name associated with the provided IP
 * @throws NamingException
 *             If a NamingException is encountered
 */
public static String reverseDns(InetAddress hostIp, String ns) throws NamingException {
    //
    // Builds the reverse IP lookup form
    // This is formed by reversing the IP numbers and appending in-addr.arpa
    //
    String[] parts = hostIp.getHostAddress().split("\\.");
    String reverseIP = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0] + ".in-addr.arpa";

    DirContext ictx = new InitialDirContext();
    Attributes attribute = ictx.getAttributes("dns://" // Use "dns:///" if the default
            + ((ns == null) ? "" : ns) +
            // nameserver is to be used
            "/" + reverseIP, new String[] { "PTR" });
    ictx.close();

    String hostname = attribute.get("PTR").get().toString();
    int hostnameLength = hostname.length();
    if (hostname.charAt(hostnameLength - 1) == '.') {
        hostname = hostname.substring(0, hostnameLength - 1);
    }
    return hostname;
}

From source file:de.jaetzold.networking.SimpleServiceDiscovery.java

/**
  * Comma separated List of IP adresses of available network interfaces
  * @param useIPv4/*from  w w w . j  av a  2s. c  o  m*/
  * @return
  */
public static String getIPAddress(boolean useIPv4) {

    String addresses = "";
    try {
        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)
                            addresses += sAddr + ", ";
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            if (delim < 0)
                                addresses += sAddr + ", ";
                            else
                                addresses += sAddr.substring(0, delim) + ", ";
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    if (addresses == null || addresses.length() <= 3)
        return "";
    return addresses.subSequence(0, addresses.length() - 2).toString();
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static String getLocalIP(boolean useIPv4) {
    String ip = "No Local IP assigned";
    try {/*from  w w  w .ja  v  a  2  s .  c o  m*/

        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : interfaces) {
            List<InetAddress> addresses = Collections.list(ni.getInetAddresses());
            for (InetAddress ia : addresses) {
                if (ia != null && !ia.isLoopbackAddress()) {
                    String sAddr = ia.getHostAddress().toUpperCase(Locale.ENGLISH);
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4) {
                            ip = sAddr;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%');
                            ip = delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        ip = "Unknown Error, Report it!";
    }
    return ip;
}

From source file:Main.java

public static String getHostIP() {
    String hostIp = null;/*from   ww w. j a v a  2 s.  c o  m*/
    try {
        Enumeration nis = NetworkInterface.getNetworkInterfaces();
        InetAddress ia = null;
        while (nis.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) nis.nextElement();
            Enumeration<InetAddress> ias = ni.getInetAddresses();
            while (ias.hasMoreElements()) {
                ia = ias.nextElement();
                if (ia instanceof Inet6Address) {
                    continue;// skip ipv6
                }
                String ip = ia.getHostAddress();
                if (!"127.0.0.1".equals(ip)) {
                    hostIp = ia.getHostAddress();
                    break;
                }
            }
        }
    } catch (SocketException e) {
        Log.i("yao", "SocketException");
        e.printStackTrace();
    }
    return hostIp;

}