Java IP Address Get getLocalIPAddresses()

Here you can find the source of getLocalIPAddresses()

Description

get Local IP Addresses

License

Apache License

Exception

Parameter Description
SocketException in case of I/O Errors

Return

A list of all the IP addresses of the current machine

Declaration

public static List<String> getLocalIPAddresses() throws SocketException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2009, 2015, Danilo Pianini and contributors
 * listed in the project's build.gradle or pom.xml file.
 *
 * This file is distributed under the terms of the Apache License, version 2.0
 *******************************************************************************/

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**//from   ww  w.  ja v  a2s. c o m
     * @return A list of all the IP addresses of the current machine
     * @throws SocketException
     *             in case of I/O Errors
     */
    public static List<String> getLocalIPAddresses() throws SocketException {
        final ArrayList<String> iplist = new ArrayList<String>(1);
        final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            final NetworkInterface ni = interfaces.nextElement();
            final Enumeration<InetAddress> ipaddresses = ni.getInetAddresses();
            while (ipaddresses.hasMoreElements()) {
                final String ip = ipaddresses.nextElement().getHostAddress();
                if (ip.contains(".") && !ip.contains("127.0.0.")) {
                    iplist.add(ip);
                }
            }
        }
        iplist.trimToSize();
        return iplist;
    }
}

Related

  1. getLocalIPAddress()
  2. getLocalIPAddress()
  3. getLocalIpAddress()
  4. getLocalIPAddress()
  5. getLocalIpAddress()
  6. getLocalIPAddresses()
  7. getLocalIpAddresses()
  8. getLocalIpByInterfaceName(String interfaceName)
  9. getLocalIPByNetworkInterface()