List of usage examples for java.net NetworkInterface getInterfaceAddresses
public java.util.List<InterfaceAddress> getInterfaceAddresses()
From source file:org.cc86.MMC.client.Main.java
public static String serverDiscovery() { String res = "0.0.0.0"; DatagramSocket c;//w w w .jav a 2 s . co m // Find the server using UDP broadcast try { //Open a random port to send the package c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "DISCOVER_MMC_REQUEST".getBytes(); //Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 0xCC86); c.send(sendPacket); l.info("Request packet sent to: 255.255.255.255 (DEFAULT)"); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface 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) { } l.info("Request packet sent to: " + broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()); } } l.info("Done looping over all network interfaces. Now waiting for a reply!"); //Wait for a response byte[] recvBuf = new byte[15000]; DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); c.receive(receivePacket); //We have a response l.info("Broadcast response from server: " + receivePacket.getAddress().getHostAddress()); //Check if the message is correct String message = new String(receivePacket.getData()).trim(); if (message.equals("DISCOVER_MMC_RESPONSE")) { //DO SOMETHING WITH THE SERVER'S IP (for example, store it in your controller) res = (receivePacket.getAddress() + "").substring(1); } //Close the port! c.close(); } catch (IOException ex) { } return res; }
From source file:Main.java
public static void printParameter(NetworkInterface ni) throws SocketException { System.out.println(" Name = " + ni.getName()); System.out.println(" Display Name = " + ni.getDisplayName()); System.out.println(" Is up = " + ni.isUp()); System.out.println(" Support multicast = " + ni.supportsMulticast()); System.out.println(" Is loopback = " + ni.isLoopback()); System.out.println(" Is virtual = " + ni.isVirtual()); System.out.println(" Is point to point = " + ni.isPointToPoint()); System.out.println(" Hardware address = " + ni.getHardwareAddress()); System.out.println(" MTU = " + ni.getMTU()); System.out.println("\nList of Interface Addresses:"); List<InterfaceAddress> list = ni.getInterfaceAddresses(); Iterator<InterfaceAddress> it = list.iterator(); while (it.hasNext()) { InterfaceAddress ia = it.next(); System.out.println(" Address = " + ia.getAddress()); System.out.println(" Broadcast = " + ia.getBroadcast()); System.out.println(" Network prefix length = " + ia.getNetworkPrefixLength()); System.out.println(""); }//ww w . j av a 2 s .c om }
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 w ww . j a v a2 s . c om*/ //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:Main.java
private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException { System.out.printf("Display name: %s%n", netint.getDisplayName()); System.out.printf("Name: %s%n", netint.getName()); Enumeration<InetAddress> inetAddresses = netint.getInetAddresses(); for (InetAddress inetAddress : Collections.list(inetAddresses)) { System.out.printf("InetAddress: %s%n", inetAddress); }// w ww . java 2s. c o m System.out.printf("Parent: %s%n", netint.getParent()); System.out.printf("Up? %s%n", netint.isUp()); System.out.printf("Loopback? %s%n", netint.isLoopback()); System.out.printf("PointToPoint? %s%n", netint.isPointToPoint()); System.out.printf("Supports multicast? %s%n", netint.isVirtual()); System.out.printf("Virtual? %s%n", netint.isVirtual()); System.out.printf("Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress())); System.out.printf("MTU: %s%n", netint.getMTU()); List<InterfaceAddress> interfaceAddresses = netint.getInterfaceAddresses(); for (InterfaceAddress addr : interfaceAddresses) { System.out.printf("InterfaceAddress: %s%n", addr.getAddress()); } System.out.printf("%n"); Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces(); for (NetworkInterface networkInterface : Collections.list(subInterfaces)) { System.out.printf("%nSubInterface%n"); displayInterfaceInformation(networkInterface); } System.out.printf("%n"); }
From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java
/** * Returns the set of broadcast addresses available to the network interfaces of this host. In case of IPv6 the set * contains the IPv6 multicast address to reach all nodes on the local link. Moreover, all addresses of the loopback * interfaces are added to the set.//from ww w .jav a2 s.c om * * @return (possibly empty) set of broadcast addresses reachable by this host */ private static Set<InetAddress> getBroadcastAddresses() { final Set<InetAddress> broadcastAddresses = new HashSet<InetAddress>(); // get all network interfaces Enumeration<NetworkInterface> ie = null; try { ie = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Could not collect network interfaces of host", e); return broadcastAddresses; } while (ie.hasMoreElements()) { NetworkInterface nic = ie.nextElement(); try { if (!nic.isUp()) { continue; } if (nic.isLoopback()) { for (InterfaceAddress adr : nic.getInterfaceAddresses()) { broadcastAddresses.add(adr.getAddress()); } } else { // check all IPs bound to network interfaces for (InterfaceAddress adr : nic.getInterfaceAddresses()) { if (adr == null) { continue; } // collect all broadcast addresses if (USE_IPV6) { try { final InetAddress interfaceAddress = adr.getAddress(); if (interfaceAddress instanceof Inet6Address) { final Inet6Address ipv6Address = (Inet6Address) interfaceAddress; final InetAddress multicastAddress = InetAddress.getByName(IPV6MULTICASTADDRESS + "%" + Integer.toString(ipv6Address.getScopeId())); broadcastAddresses.add(multicastAddress); } } catch (UnknownHostException e) { LOG.error(e); } } else { final InetAddress broadcast = adr.getBroadcast(); if (broadcast != null) { broadcastAddresses.add(broadcast); } } } } } catch (SocketException e) { LOG.error("Socket exception when checking " + nic.getName() + ". " + "Ignoring this device.", e); } } return broadcastAddresses; }
From source file:projetohorus.TamanhoHost.java
void TamanhoHost() throws UnknownHostException, SocketException { InetAddress localHost = Inet4Address.getLocalHost(); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); short x = networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); String n = localHost.getHostAddress() + "/" + x; System.out.println(n);/*from w ww . ja v a2 s . co m*/ SubnetUtils utils = new SubnetUtils(n); System.out.println(utils.getInfo()); String first = utils.getInfo().getLowAddress(); String last = utils.getInfo().getHighAddress(); String[] iplast = last.split(Pattern.quote(".")); String[] ipfirst = first.split(Pattern.quote(".")); numfirst = Integer.parseInt(ipfirst[3]); numlast = Integer.parseInt(iplast[3]); }
From source file:com.chiralBehaviors.slp.hive.configuration.BroadcastConfiguration.java
InetSocketAddress getBroadcastAddress(NetworkInterface networkInterface) { InetSocketAddress broadcastAddress = null; for (InterfaceAddress addr : networkInterface.getInterfaceAddresses()) { if (ipv4) { if (addr.getBroadcast() != null && addr.getBroadcast().getAddress().length == 4) { broadcastAddress = new InetSocketAddress(addr.getBroadcast(), port); break; }//w w w .j a v a 2s.com } else { if (addr.getBroadcast() != null && addr.getBroadcast().getAddress().length > 4) { broadcastAddress = new InetSocketAddress(addr.getBroadcast(), port); break; } } } return broadcastAddress; }
From source file:projetohorus.DadosColetadosPDF.java
void GerarPDF() throws IOException, DocumentException, EmailException { Document doc = null;/*from w ww . j av a2 s . c o m*/ OutputStream os = null; try { doc = new Document(PageSize.A4, 72, 72, 72, 72); os = new FileOutputStream("tesfinal11.pdf"); PdfWriter.getInstance(doc, os); doc.open(); Image img = Image.getInstance("LogoProject.png"); img.setAlignment(Element.ALIGN_CENTER); doc.add(img); InetAddress localHost = Inet4Address.getLocalHost(); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); short x = networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); String n = localHost.getHostAddress() + "/" + x; SubnetUtils utils = new SubnetUtils(n); ; PdfPTable table = new PdfPTable(new float[] { 0.50f, 0.70f, 0.90f }); table = new PdfPTable(3); Paragraph p = new Paragraph(""); p.setAlignment(Element.ALIGN_CENTER); p.setSpacingAfter(30); doc.add(p); p = new Paragraph("Informaes da Network"); p.setAlignment(Element.ALIGN_CENTER); p.setSpacingAfter(30); doc.add(p); Paragraph paragraph = new Paragraph("" + utils.getInfo()); paragraph.setAlignment(Element.ALIGN_CENTER); paragraph.setSpacingAfter(30); doc.add(paragraph); table.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell header = new PdfPCell(new Paragraph("Diagnostico do Scanner da Rede")); header.setColspan(3); table.addCell(header); table.addCell("IP"); table.addCell("HostName"); table.addCell("Portas Abertas"); for (int i = 0; i < IP.size(); i++) { table.addCell(IP.get(i)); table.addCell(NameHost.get(i)); table.addCell("" + PortasA.get(i)); } doc.add(table); } finally { if (doc != null) { doc.close(); } if (os != null) { os.close(); } } EnvioEmail sc = new EnvioEmail(); sc.EnvioEmail(); }
From source file:org.openhab.binding.opensprinkler.discovery.OpenSprinklerDiscoveryService.java
/** * Provide a string list of all the IP addresses associated with the network interfaces on * this machine./* www. j a v a 2 s. c o m*/ * * @return String list of IP addresses. * @throws UnknownHostException * @throws SocketException */ private List<String> getIpAddressScanList() throws UnknownHostException, SocketException { List<String> results = new ArrayList<String>(); InetAddress localHost = InetAddress.getLocalHost(); NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { InetAddress ipAddress = address.getAddress(); String cidrSubnet = ipAddress.getHostAddress() + "/" + DISCOVERY_SUBNET_MASK; /* Apache Subnet Utils only supports IP v4 for creating string list of IP's */ if (ipAddress instanceof Inet4Address) { logger.debug("Found interface IPv4 address to scan: {}", cidrSubnet); SubnetUtils utils = new SubnetUtils(cidrSubnet); results.addAll(Arrays.asList(utils.getInfo().getAllAddresses())); } else if (ipAddress instanceof Inet6Address) { logger.debug("Found interface IPv6 address to scan: {}", cidrSubnet); } else { logger.debug("Found interface unknown IP type address to scan: {}", cidrSubnet); } } return results; }
From source file:org.chromium.ChromeSystemNetwork.java
private void getNetworkInterfaces(final CordovaArgs args, final CallbackContext callbackContext) { cordova.getThreadPool().execute(new Runnable() { @Override// ww w . j a v a 2 s . co m public void run() { try { JSONArray ret = new JSONArray(); ArrayList<NetworkInterface> interfaces = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface iface : interfaces) { if (iface.isLoopback()) { continue; } for (InterfaceAddress interfaceAddress : iface.getInterfaceAddresses()) { InetAddress address = interfaceAddress.getAddress(); if (address == null) { continue; } JSONObject data = new JSONObject(); data.put("name", iface.getDisplayName()); // Strip address scope zones for IPv6 address. data.put("address", address.getHostAddress().replaceAll("%.*", "")); data.put("prefixLength", interfaceAddress.getNetworkPrefixLength()); ret.put(data); } } callbackContext.success(ret); } catch (Exception e) { Log.e(LOG_TAG, "Error occured while getting network interfaces", e); callbackContext.error("Could not get network interfaces"); } } }); }