List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:Main.java
public static Collection<InetAddress> getAllAvailableAddresses() { Set<InetAddress> retval = new HashSet<InetAddress>(); Enumeration en;// w w w .ja v a 2 s . com try { en = NetworkInterface.getNetworkInterfaces(); if (en == null) return retval; while (en.hasMoreElements()) { NetworkInterface intf = (NetworkInterface) en.nextElement(); Enumeration<InetAddress> addrs = intf.getInetAddresses(); while (addrs.hasMoreElements()) retval.add(addrs.nextElement()); } } catch (SocketException e) { e.printStackTrace(); } return retval; }
From source file:Main.java
public static String getLocalIpAddress() throws Exception { String ipAddress = null;/* w ww .ja va2 s . co m*/ Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface e = en.nextElement(); Enumeration<InetAddress> addresses = e.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) { ipAddress = address.getHostName(); break; } } } if (ipAddress == null) { ipAddress = InetAddress.getLocalHost().getHostAddress(); } return ipAddress; }
From source file:Main.java
public static String getIpAddress() { try {/*from w w w. ja v a 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() && inetAddress instanceof Inet4Address) { // if (!inetAddress.isLoopbackAddress() && inetAddress // instanceof Inet6Address) { return inetAddress.getHostAddress().toString(); } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static InetAddress getHostAddress() { try {// w w w . j a v a 2 s. c om Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) return addr; } } } catch (SocketException e) { throw new RuntimeException(e); } return null; }
From source file:Main.java
public static String getLocalIpAddress() { try {//from ww w . jav a 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().split("[%]")[0]; } } } } catch (SocketException ex) { } return null; }
From source file:Main.java
public static String getIpInfo() { String ipInfo = null;// ww w . ja va2 s .c o m try { Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces(); LOOP: while (faces.hasMoreElements()) { Enumeration<InetAddress> addresses = faces.nextElement().getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress inetAddress = addresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { ipInfo = inetAddress.getHostAddress().toString(); break LOOP; } } } } catch (Exception e) { } if (TextUtils.isEmpty(ipInfo)) { ipInfo = ""; } return ipInfo; }
From source file:Main.java
public synchronized static Inet4Address getLocalIpAddress() { try {//from w w w. ja va2 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()) { if (inetAddress instanceof Inet4Address) { return ((Inet4Address) inetAddress); } } } } } catch (SocketException ex) { } return null; }
From source file:Main.java
public static List<NetworkInterface> getAllAvailableInterfaces() throws SocketException { List<NetworkInterface> retval = new ArrayList<NetworkInterface>(10); NetworkInterface intf;/*w w w .j a v a2 s . c om*/ for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { intf = (NetworkInterface) en.nextElement(); retval.add(intf); } return retval; }
From source file:Main.java
public static String getLocalIpAddress() { try {/* w ww . jav a 2 s. c o m*/ Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); String name = intf.getName(); if (name.compareTo("eth0") == 0 || name.compareTo("wlan0") == 0) { Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress inetAddress = enumIpAddr.nextElement(); if (inetAddress.getClass() == Inet4Address.class) { return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e("MY", ex.toString()); } return null; }
From source file:Main.java
public static String getLocalIP() { try {/*from w w w. ja v a2s .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(); } } } } catch (SocketException ex) { Log.e("bevelop", null, ex); } return ""; }