List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:edu.stanford.epad.epadws.processing.pipeline.task.EpadStatisticsTask.java
public static String getIPAddress() { String ip = ""; String ipi = ""; Enumeration e;/*w w w . java 2 s . c o m*/ try { e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); ipi = i.getHostAddress(); if (!ipi.startsWith("127") && !ipi.startsWith("192") && !ipi.startsWith("172") && !ipi.startsWith("10.") && !ipi.startsWith("0:")) ip = ipi; } } } catch (SocketException e1) { e1.printStackTrace(); } if (ip.length() == 0) return ipi; else return ip; }
From source file:ai.general.interbot.NetworkInterfaceInfo.java
/** * Collects information about the local network interfaces and returns the information as an * array of NetworkInterfaceInfo objects. * * @return Information about the local network interfaces. *///www . j a va 2 s .c o m public static ArrayList<NetworkInterfaceInfo> queryAll() { ArrayList<NetworkInterfaceInfo> infos = new ArrayList<NetworkInterfaceInfo>(); try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); String name = iface.getName(); if (name.startsWith("eth") || name.startsWith("wlan")) { NetworkInterfaceInfo info = new NetworkInterfaceInfo(name); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address instanceof Inet4Address) { info.getAddresses() .add(new IpAddress(IpAddress.Version.IPv4, inetAddressToString(address))); } else if (address instanceof Inet6Address) { info.getAddresses() .add(new IpAddress(IpAddress.Version.IPv6, inetAddressToString(address))); } } if (info.getAddresses().size() > 0) { infos.add(info); } } } } catch (SocketException e) { } return infos; }
From source file:de.madvertise.android.sdk.MadUtil.java
/** * Fetch the address of the enabled interface * /* w ww .j a v a2s .com*/ * @return ip address as string */ protected static String getLocalIpAddress() { try { 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) { if (PRINT_LOG) Log.d(MadUtil.LOG, ex.toString()); } return ""; }
From source file:com.sckftr.android.utils.net.Network.java
/** * Get IP address from first non-localhost interface * @return address or empty string/* w w w. j a v a 2 s .c om*/ */ public static String getLocalIpAddress() { try { 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()) { String ip = Formatter.formatIpAddress(inetAddress.hashCode()); Log.i("NET", "***** IP=" + ip); return ip; } } } } catch (SocketException ex) { Log.e("NET", ex.toString()); } return null; }
From source file:com.nokia.dempsy.messagetransport.tcp.TcpReceiver.java
private static InetAddress getFirstNonLocalhostInetAddress() throws SocketException { Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) netInterfaces.nextElement(); for (Enumeration<InetAddress> loopInetAddress = networkInterface.getInetAddresses(); loopInetAddress .hasMoreElements();) {// w w w.j a v a 2 s .c o m InetAddress tempInetAddress = loopInetAddress.nextElement(); if (!tempInetAddress.isLoopbackAddress() && tempInetAddress instanceof Inet4Address) return tempInetAddress; } } return null; }
From source file:net.pocketmine.server.HomeActivity.java
public static String getIPAddress(boolean useIPv4) { try {/*from w w w .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(Locale.US); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) return sAddr; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); return delim < 0 ? sAddr : sAddr.substring(0, delim); } } } } } } catch (Exception ex) { } return ha.getResources().getString(R.string.unknown); }
From source file:org.apache.axis2.transport.nhttp.ServerWorker.java
/** * Copied from transport.http of Axis2/*from w w w . j a va2 s .com*/ * * Returns the ip address to be used for the replyto epr * CAUTION: * This will go through all the available network interfaces and will try to return an ip address. * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found * then this will return this will return 127.0.0.1. * This will <b>not<b> consider IPv6 addresses. * <p/> * TODO: * - Improve this logic to genaralize it a bit more * - Obtain the ip to be used here from the Call API * * @return Returns String. * @throws java.net.SocketException */ private static String getIpAddress() throws SocketException { Enumeration e = NetworkInterface.getNetworkInterfaces(); String address = "127.0.0.1"; while (e.hasMoreElements()) { NetworkInterface netface = (NetworkInterface) e.nextElement(); Enumeration addresses = netface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress ip = (InetAddress) addresses.nextElement(); if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) { return ip.getHostAddress(); } } } return address; }
From source file:es.auth.plugin.AbstractUnitTest.java
public static String getNonLocalhostAddress() { try {/*from w ww . j a v a 2 s .c o m*/ for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { final NetworkInterface intf = en.nextElement(); if (intf.isLoopback() || !intf.isUp()) { continue; } for (final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { final InetAddress ia = enumIpAddr.nextElement(); if (ia.isLoopbackAddress() || ia instanceof Inet6Address) { continue; } return ia.getHostAddress(); } } } catch (final SocketException e) { throw new RuntimeException(e); } System.out.println("ERROR: No non-localhost address available, will use localhost"); return "localhost"; }
From source file:org.springframework.cloud.dataflow.yarn.streamappmaster.EmbeddedAppmasterTrackService.java
private static String getDefaultAddress() { Enumeration<NetworkInterface> nets; try {// w w w.ja va2 s.co m nets = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { return null; } NetworkInterface netinf; while (nets.hasMoreElements()) { netinf = nets.nextElement(); boolean skip = false; try { skip = netinf.isPointToPoint(); } catch (SocketException e) { skip = true; } if (skip) { continue; } Enumeration<InetAddress> addresses = netinf.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (!address.isAnyLocalAddress() && !address.isMulticastAddress() && !(address instanceof Inet6Address)) { return address.getHostAddress(); } } } return null; }
From source file:util.Control.java
private static InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException { Enumeration en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface i = (NetworkInterface) en.nextElement(); for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) { InetAddress addr = (InetAddress) en2.nextElement(); if (!addr.isLoopbackAddress()) { if (addr instanceof Inet4Address) { if (preferIPv6) { continue; }// ww w . j av a 2s. co m return addr; } if (addr instanceof Inet6Address) { if (preferIpv4) { continue; } return addr; } } } } return null; }