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.espe.distribuidas.protocolocajero.pc.Originador.java

public static String getOriginador(String c) {
    String origen = "";
    try {/*from  ww w .j av  a  2s . c o  m*/
        InetAddress localHost = InetAddress.getLocalHost();
        origen = localHost.getHostAddress();
    } catch (UnknownHostException ex) {
        Logger.getLogger(Originador.class.getName()).log(Level.SEVERE, null, ex);
    }
    origen = origen + "@" + c;
    origen = StringUtils.rightPad(origen, 20, "0");
    return origen;
}

From source file:Main.java

public static String getHostAddress() throws UnknownHostException {
    InetAddress address = null;
    address = InetAddress.getLocalHost();
    return address.getHostAddress();
}

From source file:Main.java

public static String GetWantedIPAdress_JAVA() {
    String wantedIP = "";
    Enumeration<NetworkInterface> e = null;

    try {//  w  ww.j a va2s.c  o  m
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                if (i.getHostAddress().toString().contains("192.168.")
                        || i.getHostAddress().toString().startsWith("10.")) {
                    wantedIP = i.getHostAddress();
                    break;
                }
            }
        }
    }

    return wantedIP;
}

From source file:Main.java

public static String findAppropriateIp() {
    String result = "unknown";

    Enumeration<NetworkInterface> e;
    try {/*from   w  w  w.j  a va 2s  . c o m*/
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        return result;
    }
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress inetAddr = ee.nextElement();
            String ipAddr = inetAddr.getHostAddress();

            Matcher m = ipAddrPattern.matcher(ipAddr);
            if (m.matches()) {
                if (!m.group(1).equals("127")) {
                    result = ipAddr;
                }
            }
        }
    }

    return result;
}

From source file:Main.java

public static String GetIPAdresses_JAVA() {
    String allIPAdresses = "";
    Enumeration<NetworkInterface> e = null;

    try {//from   w  w  w.j av  a 2 s. c o m
        e = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e1) {
        e1.printStackTrace();
    }

    if (e != null) {
        while (e.hasMoreElements()) {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements()) {
                InetAddress i = (InetAddress) ee.nextElement();
                allIPAdresses += (i.getHostAddress() + "\n");
            }
        }
    }

    return allIPAdresses;
}

From source file:com.surfs.storage.common.util.CmdUtils.java

public static String getLocalhostIp() {
    try {/*  w w w . j a va  2  s. co m*/
        InetAddress inet = InetAddress.getLocalHost();
        return inet.getHostAddress();
    } catch (UnknownHostException e) {
        throw new NullPointerException("local ip is empty");
    }
}

From source file:com.netsteadfast.greenstep.util.HostUtils.java

public static String getHostAddress() {
    String hostAddress = "";
    try {//from   w  ww. java2s . c  o  m
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        for (; nics.hasMoreElements() && "".equals(hostAddress);) {
            NetworkInterface interfece = nics.nextElement();
            if (interfece.getName().toLowerCase().startsWith("lo")) {
                continue;
            }
            Enumeration<InetAddress> addrs = interfece.getInetAddresses();
            for (; addrs.hasMoreElements() && "".equals(hostAddress);) {
                InetAddress addr = addrs.nextElement();
                if (addr.getHostAddress().indexOf(":") > -1) {
                    continue;
                }
                hostAddress = addr.getHostAddress();
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    if (StringUtils.isBlank(hostAddress)) {
        hostAddress = "127.0.0.1";
    }
    return hostAddress;
}

From source file:Main.java

public static String getIPAddr() {
    try {// w  ww . jav  a2  s  .c  om
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {

            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                return inetAddress.getHostAddress().toString();
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getIP() {
    try {/*from ww w  .ja  va  2 s .  c  o  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 addr1 = addr.getHostAddress().toUpperCase().replaceAll("^/", "");
                    if (InetAddressUtils.isIPv4Address(addr1)) {
                        return addr1;
                    }
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*  ww w .j av a2s  . co  m*/
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress() {
    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 (isIPv4)
                        return sAddr;
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}