Java IP Address Get getIPs()

Here you can find the source of getIPs()

Description

get I Ps

License

Open Source License

Declaration

public static String getIPs() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.SocketException;
import java.net.NetworkInterface;

import java.net.InetAddress;
import java.net.Inet4Address;
import java.util.Enumeration;

public class Main {
    public static String getIPs() {
        try {// ww w .  j  a  v  a 2s . c o m
            Enumeration<NetworkInterface> nets;
            NetworkInterface n;
            Enumeration<InetAddress> addrs;
            InetAddress a;

            String s = "";
            nets = NetworkInterface.getNetworkInterfaces();
            while (nets.hasMoreElements()) {
                n = nets.nextElement();
                if (n.isUp()) {
                    s += n.getName() + "\n";
                    addrs = n.getInetAddresses();
                    while (addrs.hasMoreElements()) {
                        a = addrs.nextElement();
                        if (a instanceof Inet4Address) {
                            s += " * " + a.getHostAddress() + "\n";
                        }
                    }
                }
            }

            return s.trim();
        } catch (SocketException e) {
            System.out
                    .println("Error: couldn't get ip addresses for network interfaces");
            return "";
        }
    }
}

Related

  1. getIPFromNetworkInterface()
  2. getIpHostnameLocal()
  3. getIPList(String address)
  4. getIPList(String s, String s1)
  5. getIPs()
  6. getIPv4Address()
  7. getIPV4Address(List addresses)
  8. getIPv4Address(NetworkInterface iface)
  9. getIPV4MainAddress(NetworkInterface ni)