List of utility methods to do IP Address Get
List | getIPAddresses() List all IP addresses of the device, except loopback addresses. List<InetAddress> addresses = new ArrayList<InetAddress>(); List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress a : addrs) { if (a.isLoopbackAddress()) { continue; addresses.add(a); return addresses; |
InetAddress | getIpAddressesFromNic() Tries to retrieve the IP address from the NIC. try { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); while (nets.hasMoreElements()) { NetworkInterface nextElement = nets.nextElement(); String name = nextElement.getName(); if (name.startsWith("eth")) { Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses(); while (inetAddresses.hasMoreElements()) { ... |
String | getIpAddressExternal() get Ip Address External URL myIP; try { myIP = new URL("http://api.externalip.net/ip/"); BufferedReader in = new BufferedReader(new InputStreamReader(myIP.openStream())); return in.readLine(); } catch (Exception e) { try { myIP = new URL("http://myip.dnsomatic.com/"); ... |
String | getIPAddressFromBytes(byte[] bytes) get IP Address From Bytes String address = "/unresolved"; try { address = InetAddress.getByAddress(bytes).toString(); } catch (Exception e) { return address; |
InetAddress | getIPAddressFromNetworkInterface(String ifaceName) Returns an InetAddress object encapsulating what is most likely the machine's LAN IP address.
NetworkInterface iface = NetworkInterface.getByName(ifaceName); if (iface == null) { throw new IllegalArgumentException("Network interface " + ifaceName + " not found"); InetAddress candidateAddress = null; Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); while (inetAddrs.hasMoreElements()) { InetAddress inetAddr = inetAddrs.nextElement(); ... |
String | getIPAddressFromNetworkInterfaces() get IP Address From Network Interfaces Vector<String> IPs = new Vector<String>(); try { NetworkInterface iface = null; for (Enumeration<?> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) { iface = (NetworkInterface) ifaces.nextElement(); InetAddress ia = null; for (Enumeration<?> ips = iface.getInetAddresses(); ips.hasMoreElements();) { ia = (InetAddress) ips.nextElement(); ... |
InetAddress[] | getIPAdresses() get IP Adresses List<InetAddress> addrList = new ArrayList<InetAddress>(); for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics .hasMoreElements();) { NetworkInterface nic = nics.nextElement(); if (nic.isUp()) { for (Enumeration<InetAddress> addresses = nic.getInetAddresses(); addresses.hasMoreElements();) { InetAddress address = addresses.nextElement(); addrList.add(address); ... |
String | getIpByHost(String hostName) get Ip By Host try { return InetAddress.getByName(hostName).getHostAddress(); } catch (UnknownHostException e) { return hostName; |
String | getIpByHost(String hostName) get Ip By Host try { return InetAddress.getByName(hostName).getHostAddress(); } catch (UnknownHostException e) { return hostName; |
String | getIpByHost(String hostName) get Ip By Host try { String ip = hostIpCache.get(hostName); if (ip == "" || "".equals(hostName.trim())) { ip = InetAddress.getByName(hostName).getHostAddress(); hostIpCache.putIfAbsent(hostName, ip); return ip; } catch (UnknownHostException e) { ... |