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:Main.java

public static String getIpAddress(boolean alwaysGetWifi) {

    try {//from  w ww . j  a va  2 s. c  o m
        Enumeration<NetworkInterface> enmNetI = NetworkInterface.getNetworkInterfaces();
        while (enmNetI.hasMoreElements()) {
            NetworkInterface networkInterface = enmNetI.nextElement();
            boolean b = alwaysGetWifi
                    ? networkInterface.getDisplayName().equals("wlan0")
                            || networkInterface.getDisplayName().equals("eth0")
                    : true;
            if (b) {
                Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
                while (inetAddressEnumeration.hasMoreElements()) {
                    InetAddress inetAddress = inetAddressEnumeration.nextElement();
                    if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                        return "net name is " + networkInterface.getDisplayName() + ",ip is "
                                + inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

protected static void obtainHostInfo() {
    HOST_IPADDRESS = "127.0.0.1";
    HOST_NAME = "";

    try {//from   ww  w .ja  va2 s  .  co  m
        InetAddress primera = InetAddress.getLocalHost();
        String hostname = InetAddress.getLocalHost().getHostName();

        if (!primera.isLoopbackAddress() && !primera.getHostName().equalsIgnoreCase("localhost")
                && primera.getHostAddress().indexOf(':') == -1) {
            // Get it without delay!!

            //System.out.println ("we have it!");
            HOST_IPADDRESS = primera.getHostAddress();
            HOST_NAME = hostname;
            //System.out.println (hostname + " IP " + HOST_IPADDRESS);
            return;
        }

        // Get it by slow way ...
        InetAddress[] familia = InetAddress.getAllByName(hostname);

        int netto = 1;
        for (int aa = 0; aa < familia.length; aa++) {
            // System.out.println ("Networko " + netto++);
            InetAddress laAdd = familia[aa];
            String ipstring = laAdd.getHostAddress();
            hostname = laAdd.getHostName(); // don't know if it can change, probably not

            // System.out.println ("checking : " + hostname + " IP " + ipstring);
            if (laAdd.isLoopbackAddress())
                continue;
            if (hostname.equalsIgnoreCase("localhost"))
                continue;
            if (ipstring.indexOf(':') >= 0)
                continue;

            // System.out.println ("this pass!");
            HOST_IPADDRESS = ipstring;
            HOST_NAME = hostname;
            break;
        }
    } catch (Exception e) {
    }
}

From source file:com.eviware.soapui.impl.wsdl.support.http.ProxyUtils.java

private static String nslookup(String s, boolean ip) {

    InetAddress host;
    String address;/*from   www  . j  a va  2  s  . c  om*/

    // get the bytes of the IP address
    try {
        host = InetAddress.getByName(s);
        if (ip)
            address = host.getHostAddress();
        else
            address = host.getHostName();
    } catch (UnknownHostException ue) {
        return s; // no host
    }

    return address;

}

From source file:com.zq.fin.seckill.web.BaseController.java

 /**
 * ??ip//w  w  w.  j av  a2  s .  c  om
 * @param request
 * @return
 */
public static String getIpAddr(HttpServletRequest request){
      
   String ipAddress = request.getHeader("x-forwarded-for");
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getHeader("WL-Proxy-Client-IP");
   }
   if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
      ipAddress = request.getRemoteAddr();
      if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
         //????IP
         InetAddress inet=null;
         try {
            inet = InetAddress.getLocalHost();
         } catch (UnknownHostException e) {
            e.printStackTrace();
         }
         ipAddress= inet.getHostAddress();
      }
   }
   //?IPIP,IP','
   if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
      if(ipAddress.indexOf(",")>0){
         ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
      }
   }
      
   return ipAddress; 
}

From source file:net.grinder.util.NetworkUtil.java

/**
 * Get local address by connecting to a server.
 * //from  w  w  w  .j  av a 2 s. co m
 * @param byConnecting
 *            the server address to connect.
 * @param port
 *            the port to connect
 * @return IP address local IP address
 */
public static String getLocalHostAddress(String byConnecting, int port) {
    InetAddress addr = getLocalInetAddress(byConnecting, port);
    if (addr != null) {
        return addr.getHostAddress();
    } else {
        // It's final...
        return "127.0.0.1";
    }
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * Get IP address from first non-localhost interface
 * //from  w w w .  ja  va2  s  .c om
 * @param ipv4 true=return ipv4, false=return ipv6
 * @return address or empty string
 */
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();
                    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 e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.newrelic.agent.deps.org.apache.http.conn.ssl.DefaultHostnameVerifier.java

static String normaliseAddress(final String hostname) {
    if (hostname == null) {
        return hostname;
    }//from www  .j  a  v a  2  s .c  o  m
    try {
        final InetAddress inetAddress = InetAddress.getByName(hostname);
        return inetAddress.getHostAddress();
    } catch (final UnknownHostException unexpected) { // Should not happen, because we check for IPv6 address above
        return hostname;
    }
}

From source file:com.groupon.odo.proxylib.Utils.java

/**
 * This function returns the first external IP address encountered
 *
 * @return IP address or null/*from   www  .  ja  va  2 s . c  o m*/
 * @throws Exception
 */
public static String getPublicIPAddress() throws Exception {
    final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";

    String ipAddr = null;
    Enumeration e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface n = (NetworkInterface) e.nextElement();
        Enumeration ee = n.getInetAddresses();
        while (ee.hasMoreElements()) {
            InetAddress i = (InetAddress) ee.nextElement();

            // Pick the first non loop back address
            if ((!i.isLoopbackAddress() && i.isSiteLocalAddress()) || i.getHostAddress().matches(IPV4_REGEX)) {
                ipAddr = i.getHostAddress();
                break;
            }
        }
        if (ipAddr != null) {
            break;
        }
    }

    return ipAddr;
}

From source file:de.undercouch.gradle.tasks.download.TestBase.java

/**
 * Get a site local IP4 address from the current node's interfaces
 * @return the IP address or <code>null</code> if the address
 * could not be obtained/*from  w  w w  .  j  ava  2s  .  c o m*/
 * @throws SocketException if an I/O error occurs
 */
private static String findSiteLocal() throws SocketException {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface n = interfaces.nextElement();
        Enumeration<InetAddress> addresses = n.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress i = addresses.nextElement();
            if (i.isSiteLocalAddress() && i instanceof Inet4Address) {
                return i.getHostAddress();
            }
        }
    }
    return null;
}

From source file:de.codesourcery.geoip.trace.TracePath.java

protected static String getIPAddress(String name) {
    try {/*from   w  ww .jav  a  2  s. com*/
        InetAddress inet = Inet4Address.getByName(name);
        if (inet != null) {
            return inet.getHostAddress();
        }
    } catch (Exception e) {

    }
    try {
        InetAddress inet = Inet6Address.getByName(name);
        if (inet != null) {
            return inet.getHostAddress();
        }
    } catch (Exception e) {
    }
    return null;
}