Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

The list of methods to do IP Address Get are organized into topic(s).

Method

ListgetIPAddresses()
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;
InetAddressgetIpAddressesFromNic()
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()) {
...
StringgetIpAddressExternal()
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/");
...
StringgetIPAddressFromBytes(byte[] bytes)
get IP Address From Bytes
String address = "/unresolved";
try {
    address = InetAddress.getByAddress(bytes).toString();
} catch (Exception e) {
return address;
InetAddressgetIPAddressFromNetworkInterface(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();
...
StringgetIPAddressFromNetworkInterfaces()
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);
...
StringgetIpByHost(String hostName)
get Ip By Host
try {
    return InetAddress.getByName(hostName).getHostAddress();
} catch (UnknownHostException e) {
    return hostName;
StringgetIpByHost(String hostName)
get Ip By Host
try {
    return InetAddress.getByName(hostName).getHostAddress();
} catch (UnknownHostException e) {
    return hostName;
StringgetIpByHost(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) {
...