List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:org.apereo.portal.PortalInfoProviderImpl.java
protected String getNetworkInterfaceName(String networkInterfaceName) { if (networkInterfaceName == null) { return null; }// ww w .j av a 2 s . com this.logger.info("Attempting to resolve serverName using NetworkInterface named ({})", networkInterfaceName); final NetworkInterface networkInterface; try { networkInterface = NetworkInterface.getByName(networkInterfaceName); } catch (SocketException e) { logger.warn("Failed to get NetworkInterface for name (" + networkInterfaceName + ").", e); return null; } if (networkInterface == null) { logger.warn("No NetworkInterface could be found for name (" + networkInterfaceName + "). Available interface names: " + getNetworkInterfaceNames()); return null; } final Enumeration<InetAddress> inetAddressesEnum = networkInterface.getInetAddresses(); if (!inetAddressesEnum.hasMoreElements()) { logger.warn("NetworkInterface (" + networkInterface.getName() + ") has no InetAddresses to get a name from."); return null; } final InetAddress inetAddress = inetAddressesEnum.nextElement(); if (inetAddressesEnum.hasMoreElements()) { logger.warn("NetworkInterface (" + networkInterface.getName() + ") has more than one InetAddress, the hostName of the first will be returned."); } return inetAddress.getHostName(); }
From source file:com.lfv.yada.net.client.ClientNetworkManager.java
private InetAddress getLocalHostFix() throws SocketException { // Hack for getting the localhost inetaddress on linux systems Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) e.nextElement(); Enumeration e2 = networkInterface.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ip = (InetAddress) e2.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) { return ip; }/*from w w w . j a v a 2s . c om*/ } } return null; }
From source file:com.cloud.utils.net.NetUtils.java
public static InetAddress[] getInterfaceInetAddresses(final String ifName) { final List<InetAddress> addrList = new ArrayList<InetAddress>(); try {//from w w w .ja va 2 s. c om for (final NetworkInterface ifc : IteratorUtil .enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) { if (ifc.isUp() && !ifc.isVirtual() && ifc.getName().equals(ifName)) { for (final InetAddress addr : IteratorUtil.enumerationAsIterable(ifc.getInetAddresses())) { addrList.add(addr); } } } } catch (final SocketException e) { s_logger.warn("SocketException in getAllLocalInetAddresses().", e); } final InetAddress[] addrs = new InetAddress[addrList.size()]; if (addrList.size() > 0) { System.arraycopy(addrList.toArray(), 0, addrs, 0, addrList.size()); } return addrs; }
From source file:com.taobao.diamond.client.impl.DefaultDiamondPublisher.java
private String getHostAddress() { String address = "127.0.0.1"; try {/*w w w .ja v a 2 s. c om*/ 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) { log.error("", e); } return address; }
From source file:de.mangelow.throughput.NotificationService.java
private String getIPAddress() { try {//from w w w. j a v a2 s . c o m String ipv4; List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); if (nilist.size() > 0) { for (NetworkInterface ni : nilist) { List<InetAddress> ialist = Collections.list(ni.getInetAddresses()); if (ialist.size() > 0) { for (InetAddress address : ialist) { if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) { return ipv4; } } } } } } catch (SocketException ex) { if (D) ex.printStackTrace(); } return ""; }
From source file:com.material.katha.wifidirectmp3.DeviceDetailFragment.java
public byte[] getLocalIPAddress() { try {/*from ww w .j a v a 2s .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 inetAddress.getAddress(); } } } } } catch (SocketException ex) { // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); } catch (NullPointerException ex) { // Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex); } return null; }
From source file:org.deviceconnect.android.deviceplugin.chromecast.profile.ChromeCastMediaPlayerProfile.java
/** * IP??/*from w w w. j av a2s. co m*/ * * @param ?? * @return IP */ private String getIpAddress() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement(); Enumeration<InetAddress> ipAddrs = networkInterface.getInetAddresses(); while (ipAddrs.hasMoreElements()) { InetAddress ip = (InetAddress) ipAddrs.nextElement(); String ipStr = ip.getHostAddress(); if (!ip.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipStr)) { return ipStr; } } } } catch (SocketException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } return null; }
From source file:com.portfolio.data.attachment.FileServlet.java
@Override public void init(ServletConfig config) { /// List possible local address try {// ww w . java 2 s . c o m Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue; Enumeration<InetAddress> addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress current_addr = addresses.nextElement(); if (current_addr instanceof Inet4Address) ourIPs.add(current_addr.getHostAddress()); } } } catch (Exception e) { } servContext = config.getServletContext(); backend = config.getServletContext().getInitParameter("backendserver"); server = config.getServletContext().getInitParameter("fileserver"); try { String dataProviderName = config.getInitParameter("dataProviderClass"); dataProvider = (DataProvider) Class.forName(dataProviderName).newInstance(); InitialContext cxt = new InitialContext(); if (cxt == null) { throw new Exception("no context found!"); } /// Init this here, might fail depending on server hosting ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/portfolio-backend"); if (ds == null) { throw new Exception("Data jdbc/portfolio-backend source not found!"); } } catch (Exception e) { e.printStackTrace(); } }
From source file:eu.stratosphere.nephele.taskmanager.TaskManager.java
/** * Find out the TaskManager's own IP address. *//* w w w.j a v a 2s .com*/ private InetAddress getTaskManagerAddress(InetSocketAddress jobManagerAddress) throws IOException { AddressDetectionState strategy = AddressDetectionState.ADDRESS; while (true) { Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = e.nextElement(); Enumeration<InetAddress> ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = ee.nextElement(); switch (strategy) { case ADDRESS: if (hasCommonPrefix(jobManagerAddress.getAddress().getAddress(), i.getAddress())) { if (tryToConnect(i, jobManagerAddress, strategy.getTimeout())) { LOG.info("Determined " + i + " as the TaskTracker's own IP address"); return i; } } break; case FAST_CONNECT: case SLOW_CONNECT: boolean correct = tryToConnect(i, jobManagerAddress, strategy.getTimeout()); if (correct) { LOG.info("Determined " + i + " as the TaskTracker's own IP address"); return i; } break; default: throw new RuntimeException("Unkown address detection strategy: " + strategy); } } } // state control switch (strategy) { case ADDRESS: strategy = AddressDetectionState.FAST_CONNECT; break; case FAST_CONNECT: strategy = AddressDetectionState.SLOW_CONNECT; break; case SLOW_CONNECT: throw new RuntimeException("The TaskManager failed to detect its own IP address"); } if (LOG.isDebugEnabled()) { LOG.debug("Defaulting to detection strategy " + strategy); } } }