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.cats.version.utils.Utils.java

public static String getLocalHostIp() {
    String regex = "[\\d]+.[\\d]+.[\\d]+.[\\d]+";
    Enumeration<?> e = null;
    try {//from   w w  w. ja  va 2 s  .co m
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration<?> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();
            String ip = i.getHostAddress();
            if (ip.matches(regex) && !ip.equals("127.0.0.1")) {
                return ip;
            }
        }
    }
    return null;
}

From source file:ntpgraphic.LineChart.java

public static void NTPClient(String[] servers, int i) {

    Properties defaultProps = System.getProperties(); //obtiene las "properties" del sistema
    defaultProps.put("java.net.preferIPv6Addresses", "true");//mapea el valor true en la variable java.net.preferIPv6Addresses
    if (servers.length == 0) {
        System.err.println("Usage: NTPClient <hostname-or-address-list>");
        System.exit(1);//from  w w w  . j a va  2 s .  c om
    }

    Promedio = 0;
    Cant = 0;
    NTPUDPClient client = new NTPUDPClient();
    // We want to timeout if a response takes longer than 10 seconds
    client.setDefaultTimeout(10000);
    try {
        client.open();
        for (String arg : servers) {
            System.out.println();
            try {
                InetAddress hostAddr = InetAddress.getByName(arg);
                System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
                TimeInfo info = client.getTime(hostAddr);
                processResponse(info, i);
            } catch (IOException ioe) {
                System.err.println(ioe.toString());
            }
        }
    } catch (SocketException e) {
        System.err.println(e.toString());
    }

    client.close();
    //System.out.println("\n Pomedio "+(Promedio/Cant));
}

From source file:io.udvi.amqp.mq.transport.connection.CAMQPConnectionManager.java

public synchronized static void initialize(String containerId) {
    if (CAMQPConnectionManager.containerId == null) {
        String hostName = "localhost";
        try {/*from  w ww  .j  a va  2  s . co m*/
            InetAddress localMachine = InetAddress.getLocalHost();
            hostName = localMachine.getHostAddress();
            log.debug("hostName: " + hostName);
        } catch (java.net.UnknownHostException uhe) {
            String errorMessage = "Caught UnknownHostException while resolving canonicalHostName: "
                    + uhe.getMessage();
            log.error(errorMessage);
            throw new CAMQPConnectionException(errorMessage);
        }
        CAMQPConnectionManager.containerId = String.format("%s@%s", containerId, hostName);
        log.info("Initialized UdviMQ endpoint ID: " + CAMQPConnectionManager.containerId);
        System.out.println("Initialized UdviMQ endpoint ID: " + CAMQPConnectionManager.containerId);
    } else {
        throw new IllegalStateException("CAMQPConnectionManager already initialized: containerId: "
                + CAMQPConnectionManager.containerId);
    }
}

From source file:net.dovemq.transport.connection.CAMQPConnectionManager.java

public synchronized static void initialize(String containerId) {
    if (CAMQPConnectionManager.containerId == null) {
        String hostName = "localhost";
        try {//w  w w .  jav  a2 s  . co  m
            InetAddress localMachine = InetAddress.getLocalHost();
            hostName = localMachine.getHostAddress();
            log.debug("hostName: " + hostName);
        } catch (java.net.UnknownHostException uhe) {
            String errorMessage = "Caught UnknownHostException while resolving canonicalHostName: "
                    + uhe.getMessage();
            log.error(errorMessage);
            throw new CAMQPConnectionException(errorMessage);
        }
        CAMQPConnectionManager.containerId = String.format("%s@%s", containerId, hostName);
        log.info("Initialized DoveMQ endpoint ID: " + CAMQPConnectionManager.containerId);
        System.out.println("Initialized DoveMQ endpoint ID: " + CAMQPConnectionManager.containerId);
    } else {
        throw new IllegalStateException("CAMQPConnectionManager already initialized: containerId: "
                + CAMQPConnectionManager.containerId);
    }
}

From source file:com.nridge.core.base.std.Platform.java

/**
 * Convenience method that returns the host IP address of the current machine.
 *
 * @return Host IP address./*from  w w w.  j a  va2  s.  co  m*/
 */
public static String getHostIPAddress() {
    String hostAddress;

    try {
        InetAddress inetAddress = InetAddress.getLocalHost();
        hostAddress = inetAddress.getHostAddress();
    } catch (UnknownHostException e) {
        hostAddress = "127.0.0.1";
    }

    return hostAddress;
}

From source file:io.galeb.router.sync.HttpClient.java

private static String localIpsEncoded() {
    final List<String> ipList = new ArrayList<>();
    try {/*  w  w w  .  ja v a  2 s .  co m*/
        Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces();
        while (ifs.hasMoreElements()) {
            NetworkInterface localInterface = ifs.nextElement();
            if (!localInterface.isLoopback() && localInterface.isUp()) {
                Enumeration<InetAddress> ips = localInterface.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress ipaddress = ips.nextElement();
                    if (ipaddress instanceof Inet4Address) {
                        ipList.add(ipaddress.getHostAddress());
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    String ip = String.join("-", ipList);
    if (ip == null || "".equals(ip)) {
        ip = "undef-" + System.currentTimeMillis();
    }
    return ip.replaceAll("[:%]", "");
}

From source file:com.github.ipaas.ifw.util.IPUtil.java

/**
 * ?IP? ??IP/*from  ww  w .  j  a  v  a  2s.  com*/
 * 
 * @param isInter
 *            ?IP19.2.
 * @return ip?
 */
public static String getLocalIP(boolean isInter) {
    String localIP = "";
    for (InetAddress ia : localAddressList) {
        String ip = ia.getHostAddress();
        // ipv6?loopback?
        if (ia instanceof Inet6Address || ip.startsWith("127")) {
            continue;
        }
        // ipv4?
        if (StringUtil.isNullOrBlank(localIP)) {
            localIP = ip;
        }
        if (isInter && ip.startsWith("19.")) {
            return ip;
        }
        if (!isInter && !ip.startsWith("19.")) {
            return ip;
        }
    }
    return localIP;
}

From source file:info.sugoiapps.xoclient.XOverClient.java

/**
 * Get the internal IP address of the client machine.
 * @return the address/*from  w  w w .  j  a  v  a  2 s  .c  o  m*/
 */
private static String getLocalAddress() {
    InetAddress addr;
    try {
        addr = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return "No address found";
    }
    return addr.getHostAddress();
}

From source file:edu.stanford.junction.provider.jx.JunctionProvider.java

public static String getLocalIpAddress() {
    try {/*from   w w w  .jav a 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()) {
                    // not ready for IPv6, apparently.
                    if (!inetAddress.getHostAddress().contains(":")) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("junction", ex.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Get all device ip addresses//from  ww w .  j a va 2s . co m
 * @return {@link array}
 */
public static String[] getLocalIpAddress() {
    ArrayList<String> addresses = new ArrayList<String>();
    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();
                if (!inetAddress.isLoopbackAddress()) {
                    //IPAddresses.setText(inetAddress.getHostAddress().toString());
                    boolean isIPv4 = isIPv4Address(inetAddress.getHostAddress().toString());
                    if (isIPv4) {
                        addresses.add(inetAddress.getHostAddress().toString());
                    }
                }
            }
        }
    } catch (SocketException ex) {
        String LOG_TAG = null;
        Log.e(LOG_TAG, ex.toString());
    }

    return addresses.toArray(new String[0]);
}