List of usage examples for java.net NetworkInterface getInetAddresses
public Enumeration<InetAddress> getInetAddresses()
From source file:de.jaetzold.networking.SimpleServiceDiscovery.java
/** * Comma separated List of IP adresses of available network interfaces * @param useIPv4//from ww w . j a v a 2s . c om * @return */ public static String getIPAddress(boolean useIPv4) { String addresses = ""; try { 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(); boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); if (useIPv4) { if (isIPv4) addresses += sAddr + ", "; } else { if (!isIPv4) { int delim = sAddr.indexOf('%'); // drop ip6 port suffix if (delim < 0) addresses += sAddr + ", "; else addresses += sAddr.substring(0, delim) + ", "; } } } } } } catch (Exception ex) { } // for now eat exceptions if (addresses == null || addresses.length() <= 3) return ""; return addresses.subSequence(0, addresses.length() - 2).toString(); }
From source file:camp.pixels.signage.util.NetworkInterfaces.java
/** * @param interfaceName eth0, wlan0 or NULL=use first interface * @param useIPv4/*from w w w . ja va 2s .c om*/ * @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:com.ghgande.j2mod.modbus.utils.TestUtils.java
/** * Returns the first real IP address it finds * * @return Real IP address or null if nothing available *//*from ww w . j a va 2s . c om*/ public static String getFirstIp4Address() { // Get all the physical adapters List<NetworkInterface> adapters = getNetworkAdapters(); if (adapters.size() > 0) { for (NetworkInterface adapter : adapters) { // Loop through all the addresses Enumeration<InetAddress> addresses = adapter.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); // Only interested in non-loopback and IPv4 types if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address.getHostAddress(); } } } } return null; }
From source file:org.kei.android.phone.cellhistory.towers.MobileNetworkInfo.java
/** Get IP For mobile */ public static String getMobileIP(final boolean useIPv4) { try {//from w w w.j av a 2 s .c om final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (final 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(Locale.US); final 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); } } } } } } catch (final Exception ex) { Log.e(MobileNetworkInfo.class.getSimpleName(), "Exception in Get IP Address: " + ex.getMessage(), ex); } return TowerInfo.UNKNOWN; }
From source file:davmail.exchange.ExchangeSessionFactory.java
/** * Check if at least one network interface is up and active (i.e. has an address) * * @return true if network available// w w w .j av a 2 s .c o m */ static boolean checkNetwork() { boolean up = false; Enumeration<NetworkInterface> enumeration; try { enumeration = NetworkInterface.getNetworkInterfaces(); while (!up && enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration.nextElement(); up = networkInterface.isUp() && !networkInterface.isLoopback() && networkInterface.getInetAddresses().hasMoreElements(); } } catch (NoSuchMethodError error) { ExchangeSession.LOGGER.debug("Unable to test network interfaces (not available under Java 1.5)"); up = true; } catch (SocketException exc) { ExchangeSession.LOGGER.error( "DavMail configuration exception: \n Error listing network interfaces " + exc.getMessage(), exc); } return up; }
From source file:org.cc86.MMC.client.Main.java
public static void serverDiscoveryHack(String[] args) { try {//from w w w .jav a2s . c om final boolean[] foundSomething = { false }; List<InetAddress> addrList = new ArrayList<InetAddress>(); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { try { NetworkInterface ifc = (NetworkInterface) interfaces.nextElement(); if (ifc.isUp()) { Enumeration addresses = ifc.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = (InetAddress) addresses.nextElement(); if (addr instanceof Inet6Address) { continue; } byte[] addrraw = addr.getAddress(); for (int i = 1; i < 254; i++) { try { final InetAddress calculated = InetAddress.getByAddress( new byte[] { addrraw[0], addrraw[1], addrraw[2], (byte) i }); new Thread(() -> { //c=new TCPConnection(calculated.getHostAddress(), 0xCC86); try { Socket client = new Socket(); client.connect( new InetSocketAddress(calculated.getHostAddress(), 0xcc87), 2000); synchronized (foundSomething) { if (!foundSomething[0]) { foundSomething[0] = true; ArrayList<String> lst = new ArrayList(Arrays.asList(args)); lst.add("-i"); lst.add(calculated.getHostAddress()); main(lst.toArray(args)); } } } catch (IOException ex) { //System.out.println(ex.m); l.trace("Dead host"); //System.exit(0); } }).start(); } catch (UnknownHostException ex) { ex.printStackTrace(); } } } } } catch (SocketException ex) { ex.printStackTrace(); } } } catch (SocketException ex) { ex.printStackTrace(); } }
From source file:com.cellbots.logger.localServer.LocalHttpServer.java
/** * Returns the IP addresses of this device. * //from w ww .ja v a 2 s . c om * @return IP addresses as a String. Addresses are separated by newline. */ public static String getLocalIpAddresses() { String ipAddresses = ""; 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 ipAddress = inetAddress.getHostAddress().toString(); if (ipAddress.split("\\.").length == 4) { ipAddresses = ipAddresses + ipAddress + ":8080\n"; } } } } } catch (SocketException ex) { Log.e("", ex.toString()); } return ipAddresses; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static String getIp() { String localip = null;// IP?IP String netip = null;// IP try {//from w ww . j av a 2 s.com Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null; boolean finded = false;// ?IP while (netInterfaces.hasMoreElements() && !finded) { NetworkInterface ni = netInterfaces.nextElement(); Enumeration<InetAddress> address = ni.getInetAddresses(); while (address.hasMoreElements()) { ip = address.nextElement(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP netip = ip.getHostAddress(); finded = true; break; } else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// IP localip = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } if (netip != null && !"".equals(netip)) { return netip; } else { return localip; } }
From source file:com.anyi.gp.license.RegisterTools.java
public static Map getInetAddresses() { Map result = new HashMap(); Enumeration netInterfaces = null; try {//w w w .ja v a2s. c om netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); String displayName = ni.getDisplayName(); String ipString = ""; Enumeration ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ipString += ((InetAddress) ips.nextElement()).getHostAddress() + ","; } if (ipString != null && ipString.length() > 0) ipString = ipString.substring(0, ipString.length() - 1); result.put(displayName, ipString); } } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:com.moss.greenshell.wizard.catastrophe.PostMortemScreen.java
public static void submitErrorReport(final Throwable cause, final ErrorReportDecorator... decorators) throws Exception { List<ErrorReportChunk> chunks = new LinkedList<ErrorReportChunk>(); try {/*from www .j ava 2s .c om*/ if (cause instanceof InternalErrorException) { InternalErrorException ie = (InternalErrorException) cause; ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain", ie.id().getBytes("UTF8")); chunks.add(chunk); } else if (cause instanceof SOAPFaultException) { SOAPFaultException soapFault = (SOAPFaultException) cause; String content = soapFault.getFault().getFirstChild().getTextContent(); String prefix = "Internal Service Error Occurred: "; if (content.startsWith(prefix)) { String id = content.substring(prefix.length()); ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain", id.getBytes("UTF8")); chunks.add(chunk); } } } catch (Throwable t) { t.printStackTrace(); } // STACK TRACE ByteArrayOutputStream stackBytes = new ByteArrayOutputStream(); PrintStream stackPrintStream = new PrintStream(stackBytes); cause.printStackTrace(stackPrintStream); stackPrintStream.close(); stackBytes.close(); ErrorReportChunk chunk = new ErrorReportChunk("stack trace", "text/plain", stackBytes.toByteArray()); chunks.add(chunk); // THREAD DUMP ByteArrayOutputStream dumpBytes = new ByteArrayOutputStream(); PrintStream out = new PrintStream(dumpBytes); Map<Thread, StackTraceElement[]> traceMap = Thread.getAllStackTraces(); for (Map.Entry<Thread, StackTraceElement[]> next : traceMap.entrySet()) { out.println(); out.println(next.getKey().getName()); for (StackTraceElement line : next.getValue()) { String className = emptyIfNull(line.getClassName()); String methodName = emptyIfNull(line.getMethodName()); String fileName = emptyIfNull(line.getFileName()); out.println(" " + className + "." + methodName + " (" + fileName + " line " + line.getLineNumber() + ")"); } } out.flush(); out.close(); ErrorReportChunk stackDump = new ErrorReportChunk("thread dump", "text/plain", dumpBytes.toByteArray()); chunks.add(stackDump); // SYSTEM PROPERTIES ByteArrayOutputStream propsBytes = new ByteArrayOutputStream(); PrintStream propsOut = new PrintStream(propsBytes); for (Map.Entry<Object, Object> next : System.getProperties().entrySet()) { propsOut.println(" " + next.getKey() + "=" + next.getValue()); } propsOut.flush(); propsOut.close(); chunks.add(new ErrorReportChunk("system properties", "text/plain", propsBytes.toByteArray())); // LOCAL CLOCK chunks.add(new ErrorReportChunk("local clock", "text/plain", new DateTime().toString().getBytes())); // NETWORKING StringBuffer networking = new StringBuffer(); Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); while (ifaces.hasMoreElements()) { NetworkInterface iface = ifaces.nextElement(); networking.append("INTERFACE: " + iface.getName() + " (" + iface.getDisplayName() + ")\n"); Enumeration<InetAddress> addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); networking.append(" Address:" + address.getHostAddress() + "\n"); networking.append(" Cannonical Host Name: " + address.getCanonicalHostName() + "\n"); networking.append(" Host Name: " + address.getHostName() + "\n"); } } chunks.add(new ErrorReportChunk("network configuration", "text/plain", networking.toString().getBytes())); // DECORATORS if (decorators != null) { for (ErrorReportDecorator decorator : decorators) { chunks.addAll(decorator.makeChunks(cause)); } } ErrorReport report = new ErrorReport(chunks); Reporter reporter = new Reporter(); ReportId id = reporter.submitReport(report); }