List of usage examples for java.net NetworkInterface getNetworkInterfaces
public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException
From source file:stargate.commons.utils.IPUtils.java
public static Collection<String> getHostAddress() { if (!cached_host_addr.isEmpty()) { return Collections.unmodifiableCollection(cached_host_addr); } else {/*from w ww . j a v a 2 s. c o m*/ try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while (e.hasMoreElements()) { NetworkInterface n = (NetworkInterface) e.nextElement(); Enumeration ee = n.getInetAddresses(); while (ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); String hostAddress = i.getHostAddress(); if (isProperHostAddress(hostAddress)) { if (!cached_host_addr.contains(hostAddress)) { cached_host_addr.add(hostAddress); } } String hostName = i.getHostName(); if (isProperHostAddress(hostName)) { if (!cached_host_addr.contains(hostName)) { cached_host_addr.add(hostName); } } String canonicalHostName = i.getCanonicalHostName(); if (isProperHostAddress(canonicalHostName)) { if (!cached_host_addr.contains(canonicalHostName)) { cached_host_addr.add(canonicalHostName); } } } } } catch (SocketException ex) { LOG.error("Exception occurred while scanning local interfaces", ex); } return Collections.unmodifiableCollection(cached_host_addr); } }
From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils.java
public static String findAvailableHostAddress() throws UnknownHostException, SocketException { InetAddress address = InetAddress.getLocalHost(); if (address.isLoopbackAddress()) { for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { if (!networkInterface.isLoopback()) { for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress a = interfaceAddress.getAddress(); if (a instanceof Inet4Address) { return a.getHostAddress(); }/*from ww w . java 2 s .c o m*/ } } } } return address.getHostAddress(); }
From source file:com.t_oster.visicut.misc.Helper.java
public static List<String> findVisiCamInstances() { List<String> result = new LinkedList<String>(); // Find the server using UDP broadcast try {//from www.j a v a 2 s . c o m //Open a random port to send the package DatagramSocket c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "VisiCamDiscover".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 8888); c.send(sendPacket); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; // Don't want to broadcast to the loopback interface } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast == null) { continue; } // Send the broadcast package! try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 8888); c.send(sendPacket); } catch (Exception e) { } } } //Wait for a response byte[] recvBuf = new byte[15000]; c.setSoTimeout(3000); while (true) { DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); try { c.receive(receivePacket); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); //Close the port! c.close(); if (message.startsWith("http")) { result.add(message); } } catch (SocketTimeoutException e) { break; } } } catch (IOException ex) { } return result; }
From source file:io.galeb.router.sync.HttpClient.java
private static String localIpsEncoded() { final List<String> ipList = new ArrayList<>(); try {//w w w . j av a2 s . co m Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); while (ifs.hasMoreElements()) { NetworkInterface localInterface = ifs.nextElement(); if (!localInterface.isLoopback() && localInterface.isUp()) { Enumeration<InetAddress> ips = localInterface.getInetAddresses(); while (ips.hasMoreElements()) { InetAddress ipaddress = ips.nextElement(); if (ipaddress instanceof Inet4Address) { ipList.add(ipaddress.getHostAddress()); break; } } } } } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } String ip = String.join("-", ipList); if (ip == null || "".equals(ip)) { ip = "undef-" + System.currentTimeMillis(); } return ip.replaceAll("[:%]", ""); }
From source file:net.sourceforge.subsonic.util.Util.java
/** * Returns the local IP address./*from w ww . j a v a2 s .c o m*/ * @return The local IP, or the loopback address (127.0.0.1) if not found. */ public static String getLocalIpAddress() { try { // Try the simple way first. InetAddress address = InetAddress.getLocalHost(); if (!address.isLoopbackAddress()) { return address.getHostAddress(); } // Iterate through all network interfaces, looking for a suitable IP. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { return addr.getHostAddress(); } } } } catch (Throwable x) { LOG.warn("Failed to resolve local IP address.", x); } return "127.0.0.1"; }
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. *//* w w w . jav a 2 s .c om*/ 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:com.chiralBehaviors.slp.hive.configuration.MulticastConfiguration.java
public NetworkInterface getNetworkInterface() throws SocketException { if (networkInterface == null) { for (Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces(); intfs .hasMoreElements();) {//from ww w . j av a 2s. com NetworkInterface intf = intfs.nextElement(); if (intf.supportsMulticast()) { return intf; } } throw new IllegalStateException("No interface supporting multicast was discovered"); } NetworkInterface iface = NetworkInterface.getByName(networkInterface); if (iface == null) { throw new IllegalArgumentException( String.format("Cannot find network interface: %s ", networkInterface)); } return iface; }
From source file:zipkin.server.brave.BraveConfiguration.java
/** This gets the lanIP without trying to lookup its name. */ // http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet @Bean//w ww .j a v a 2 s. c o m @Scope Endpoint local(@Value("${server.port:9411}") int port) { Endpoint.Builder builder = Endpoint.builder().serviceName("zipkin-server").port(port == -1 ? 0 : port); try { byte[] address = Collections.list(NetworkInterface.getNetworkInterfaces()).stream() .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) .filter(ip -> ip.isSiteLocalAddress()).findAny().get().getAddress(); if (address.length == 4) { builder.ipv4(ByteBuffer.wrap(address).getInt()); } else if (address.length == 16) { builder.ipv6(address); } } catch (Exception ignored) { builder.ipv4(127 << 24 | 1); } return builder.build(); }
From source file:at.tugraz.ist.akm.networkInterface.WifiIpAddress.java
public String readIp4ApAddress() { try {//from w w w .java 2 s .c o m for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { mLog.debug("found AP address [" + inetAddress.getHostAddress() + "]"); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { mLog.debug("failed to read ip address in access point mode"); } return null; }
From source file:com.commonsware.android.webserver.simple.WebServerService.java
private void raiseStartedEvent() { ServerStartedEvent event = new ServerStartedEvent(); try {//w w w . j ava 2 s. co m for (Enumeration<NetworkInterface> enInterfaces = NetworkInterface.getNetworkInterfaces(); enInterfaces .hasMoreElements();) { NetworkInterface ni = enInterfaces.nextElement(); for (Enumeration<InetAddress> enAddresses = ni.getInetAddresses(); enAddresses.hasMoreElements();) { InetAddress addr = enAddresses.nextElement(); if (addr instanceof Inet4Address) { event.addUrl("http://" + addr.getHostAddress() + ":4999"); } } } } catch (SocketException e) { Log.e(getClass().getSimpleName(), "Exception in IP addresses", e); } EventBus.getDefault().removeAllStickyEvents(); EventBus.getDefault().postSticky(event); }