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

SetgetAllLocalIpv4Addresses()
Get IP addresses for all interfaces on this machine that are IPv4.
Set<InetAddress> ret = new HashSet<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface networkInterface = interfaces.nextElement();
    Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
    while (addrs.hasMoreElements()) { 
        InetAddress addr = addrs.nextElement();
        if (addr instanceof Inet4Address && !addr.isAnyLocalAddress()) {
...
SetgetAllMyHostIPV4Adresses()
get All My Host IPV Adresses
Set<InetAddress> addresses = new HashSet<InetAddress>();
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
    Enumeration<InetAddress> ias = en.nextElement().getInetAddresses();
    while (ias.hasMoreElements()) {
        InetAddress address = ias.nextElement();
        if (isIPv4(stripToSlash(address.toString())))
            addresses.add(address);
...
ListgetAllUsableIPAddresses()
Returns a list of IP addresses that can be used to communicate with a peer on a different machine.
List<String> output = new ArrayList<String>();
try {
    for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        for (InterfaceAddress address : ni.getInterfaceAddresses()) {
            if (!address.getAddress().isMulticastAddress() && !address.getAddress().isLinkLocalAddress()
                    && !address.getAddress().isLoopbackAddress()) {
                output.add(address.getAddress().getHostAddress() + "/" + address.getNetworkPrefixLength());
} catch (SocketException e) {
return output;
StringgetExternalIP()
get External IP
try {
    URL myIp = new URL("http://checkip.dyndns.org/");
    BufferedReader in = new BufferedReader(new InputStreamReader(myIp.openStream()));
    String s = in.readLine();
    return s.substring(s.lastIndexOf(":") + 2, s.lastIndexOf("</body>"));
} catch (Exception ex) {
    return "error " + ex;
InetAddressgetExternalIp()
get External Ip
List<InetAddress> localInetAddresses = getLocalInetAddresses();
Pattern pattern = Pattern.compile(EXTERNAL_IP_REGEX);
for (InetAddress localInetAddress : localInetAddresses) {
    String hostAddress = localInetAddress.getHostAddress();
    if (!hostAddress.startsWith("127.") && pattern.matcher(hostAddress).matches()) {
        return localInetAddress;
return null;
StringgetExternalIp()
get External Ip
BufferedReader in = null;
try {
    URL whatismyip = new URL("http://checkip.amazonaws.com");
    in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
    String ip = in.readLine();
    return ip;
} catch (Exception exc) {
    System.out.println("Exception getting external IP: " + exc.getMessage());
...
StringgetExternalIp()
Fetches the external ip through amazonaw's utility.
try {
    URL url = new URL("http://checkip.amazonaws.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    return in.readLine();
} catch (MalformedURLException e) {
    return null;
} catch (IOException e) {
    return null;
...
StringgetExternalIP(String host, String regexPattern)
Finds external IP address by sending http request to host with expected response in pattern regexPattern
String resp = null;
if (regexPattern == null)
    regexPattern = ".+<body>Current IP Address: (\\d+.\\d+.\\d+.\\d+)</body>.+";
if (host == null)
    host = "http://checkip.dyndns.org:8245/";
Pattern pattern = Pattern.compile(regexPattern);
try {
    java.net.URL URL = new java.net.URL(host);
...
StringgetExternalIPAddress()
Returns external IP address by sending HTTP request to http://whatismyip.com/automation/n09230945.asp
String url = null;
try {
    java.net.URL URL = new java.net.URL("http://whatismyip.com/automation/n09230945.asp");
    java.net.HttpURLConnection conn = (HttpURLConnection) URL.openConnection();
    conn.setConnectTimeout(60000);
    conn.setReadTimeout(60000);
    java.io.InputStream InStream = conn.getInputStream();
    java.io.InputStreamReader Isr = new java.io.InputStreamReader(InStream);
...
StringgetExternalIPAddress()
get External IP Address
BufferedReader in = null;
try {
    URL whatismyip = new URL("http://checkip.amazonaws.com");
    URLConnection connection = whatismyip.openConnection();
    connection.addRequestProperty("Protocol", "Http/1.1");
    connection.addRequestProperty("Connection", "keep-alive");
    connection.addRequestProperty("Keep-Alive", "1000");
    connection.addRequestProperty("User-Agent", "Web-Agent");
...