List of usage examples for java.net InetAddress getHostAddress
public String getHostAddress()
From source file:com.github.christiangda.utils.ip.IP.java
/** * @param ip InetAddress/*from w w w. ja v a 2 s. co m*/ * @return boolean */ public static boolean isValidIPV4(InetAddress ip) { if (ip == null) { return false; } return InetAddressUtils.isIPv4Address(ip.getHostAddress()); }
From source file:com.seadee.degree.service.NetworkStateReceiver.java
public static String getLocalIpAddress(Context context) { final String IPTAG = "getLocalIpAddress"; String nullAddress = "0.0.0.0"; try {//from w w w . j a v a 2s. c om if (SettingVarible.networkstate != SettingVarible.NETWORKSTATE.NONETWORK) { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); String ip_address = inetAddress.getHostAddress(); if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip_address)) return ip_address; } } } } catch (SocketException ex) { Log.e(IPTAG, ex.toString()); } return nullAddress; }
From source file:cn.leancloud.diamond.server.utils.SystemConfig.java
private static String getHostAddress() { String address = "127.0.0.1"; try {// ww w . j a va 2 s. co m Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface ni = en.nextElement(); Enumeration<InetAddress> ads = ni.getInetAddresses(); while (ads.hasMoreElements()) { InetAddress ip = ads.nextElement(); if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress()) { return ip.getHostAddress(); } } } } catch (Exception e) { } return address; }
From source file:com.baidu.rigel.biplatform.cache.util.MacAddressUtil.java
/** * ??ip???ip?/*w w w . ja va2 s . c o m*/ * @param ia * @throws SocketException * @throws UnknownHostException */ public static String getIpAddress(InetAddress ia) throws UnknownHostException { if (ia == null) { ia = InetAddress.getLocalHost(); } return ia.getHostAddress(); }
From source file:camp.pixels.signage.util.NetworkInterfaces.java
/** * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4//from w w w .jav a 2s .co m * @return address or empty string */ @SuppressLint("DefaultLocale") public static String getIPAddress(String interfaceName, boolean useIPv4) { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (interfaceName != null) { if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; } if (!VALID_INTERFACES.contains(intf.getName())) continue; 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 (SocketException ex) { } return ""; }
From source file:org.namelessrom.devicecontrol.net.NetworkInfo.java
public static String getIpAddress(final boolean useIPv4) { List<NetworkInterface> interfaces; try {//w ww . j a va 2 s. c o m interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); } catch (Exception e) { interfaces = null; } if (interfaces == null) return "0.0.0.0"; for (NetworkInterface intf : interfaces) { final List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (final InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { final String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) { return sAddr; } } else { if (!isIPv4) { final int delim = sAddr.indexOf('%'); // drop ip6 port suffix return ((delim < 0) ? sAddr : sAddr.substring(0, delim)); } } } } } return "0.0.0.0"; }
From source file:Main.java
/** * Calculates the network mask address from the value * //w ww . j a v a2 s . c o m * @param numericMask * the network mask * @return */ public static String networkMaskFromInt(int numericMask) { // StringBuffer sb = new StringBuffer(15); // for (int shift = 24; shift > 0; shift -= 8) { // // process 3 bytes, from high order byte down. // sb.append(Integer.toString((numericMask >>> shift) & 0xff)); // sb.append('.'); // } // sb.append(Integer.toString(numericMask & 0xff)); // return sb.toString(); // } // // public String getMaskAsString() { int value = 0xffffffff << (32 - numericMask); byte[] bytes = new byte[] { (byte) (value >>> 24), (byte) (value >> 16 & 0xff), (byte) (value >> 8 & 0xff), (byte) (value & 0xff) }; InetAddress netAddr = null; try { netAddr = InetAddress.getByAddress(bytes); } catch (UnknownHostException e) { e.printStackTrace(); } return netAddr.getHostAddress(); }
From source file:Main.java
public static String getWifiIpv6() { try {/*from w ww . j a va 2s . 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() && inetAddress instanceof Inet6Address) { String[] ipv6 = inetAddress.getHostAddress().split("%"); String port = ipv6[1]; if (port.matches("wlan\\d+")) { return ipv6[0]; } } } } } catch (SocketException ex) { /*CONSUME*/ } return null; }
From source file:Main.java
public static String shortName(InetAddress hostname) { if (hostname == null) return null; StringBuilder sb = new StringBuilder(); if (resolve_dns) sb.append(hostname.getHostName()); else/*from w w w . j av a 2 s .c o m*/ sb.append(hostname.getHostAddress()); return sb.toString(); }
From source file:com.vinexs.tool.NetworkManager.java
public static String getLocalIPAddress() { try {/*from w w w . j a v a 2s. c om*/ InetAddress meHost = InetAddress.getLocalHost(); return meHost.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); return "127.0.0.1"; } }