List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:Main.java
public static String getLocalIpAddress() { String ret = ""; try {/* w w w . j ava 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()) { ret = inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return ret; }
From source file:Main.java
/** * Get IP address from first non-localhost interface * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string/* w w w. j a va2 s . com*/ */ 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 ex) { } // for now eat exceptions return ""; }
From source file:Main.java
@SuppressLint("DefaultLocale") public static String getLocalHostAddress() { boolean useIPv4 = true; try {//from www . j a v a 2 s . com 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
/** * Get IP address from first non-localhost interface * * @param useIPv4 true=return ipv4, false=return ipv6 * @return address or empty string// www .ja v a2s .com */ 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(); //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); boolean isIPv4 = sAddr.indexOf(':') < 0; if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 zone suffix return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase(); } } } } } } catch (Exception ex) { } // for now eat exceptions return ""; }
From source file:Main.java
public static String getLocalIPAddress() { try {// w w w .j a va 2 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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { return "0.0.0.0"; } return "0.0.0.0"; }
From source file:Main.java
public static String getLocalIpAddress() { try {/* w w w .j a v a2 s .c om*/ Enumeration<NetworkInterface> networkInfo = NetworkInterface.getNetworkInterfaces(); for (Enumeration<NetworkInterface> en = networkInfo; en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); Enumeration<InetAddress> intfAddress = intf.getInetAddresses(); for (Enumeration<InetAddress> enumIpAddr = intfAddress; enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { } return null; }
From source file:Main.java
public static String getLocalIpAddress() { try {//from w w w.ja v a2 s .co 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()) { return inetAddress.getHostAddress(); } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; }
From source file:com.github.christiangda.utils.ip.IP.java
/** * @param ip InetAddress// w w w . j av a 2 s .co m * @return boolean */ public static boolean isValidIP(InetAddress ip) { return ip != null && (InetAddressUtils.isIPv4Address(ip.getHostAddress()) || InetAddressUtils.isIPv6Address(ip.getHostAddress())); }
From source file:SocketAddressEncoder.java
public static String encode(InetSocketAddress address) { InetAddress servAddr = address.getAddress(); int servPort = address.getPort(); return servAddr.getHostAddress().replace('.', ',') + ',' + (servPort >> 8) + ',' + (servPort & 0xFF); }
From source file:com.alibaba.napoli.metamorphosis.network.RemotingUtils.java
public static String normalizeHostAddress(final InetAddress localHost) { if (localHost instanceof Inet6Address) { return "[" + localHost.getHostAddress() + "]"; } else {/*from w ww . j a v a 2s.c o m*/ return localHost.getHostAddress(); } }