Java IP Address Get getLocalIPCollection()

Here you can find the source of getLocalIPCollection()

Description

based on http://pastebin.com/5X073pUc <p> Returns all available IP addresses.

License

Apache License

Exception

Parameter Description
SocketException errors

Return

Returns all IP addresses (can be an empty list in error case or if network connection is missing).

Declaration

public static Collection<InetAddress> getLocalIPCollection() throws SocketException 

Method Source Code

//package com.java2s;
/*// ww w .j  a  v a 2  s.c  o  m
 *    Copyright 2012 Chris Fregly
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collection;
import java.util.Enumeration;

import java.util.List;

import com.google.common.collect.Lists;

public class Main {
    /**
     * based on http://pastebin.com/5X073pUc
     * <p>
     * 
     * Returns all available IP addresses.
     * <p>
     * In error case or if no network connection is established, we return an
     * empty list here.
     * <p>
     * Loopback addresses are excluded - so 127.0.0.1 will not be never
     * returned.
     * <p>
     * The "primary" IP might not be the first one in the returned list.
     * 
     * @return Returns all IP addresses (can be an empty list in error case or
     *         if network connection is missing).
     * @since 0.1.0
     * @throws SocketException
     *             errors
     */
    public static Collection<InetAddress> getLocalIPCollection() throws SocketException {
        List<InetAddress> addressList = Lists.newArrayList();
        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
        if (nifs == null) {
            return addressList;
        }

        while (nifs.hasMoreElements()) {
            NetworkInterface nif = nifs.nextElement();

            // We ignore subinterfaces - as not yet needed.

            Enumeration<InetAddress> adrs = nif.getInetAddresses();
            while (adrs.hasMoreElements()) {
                InetAddress adr = adrs.nextElement();
                if (adr != null && !adr.isLoopbackAddress()
                        && (nif.isPointToPoint() || !adr.isLinkLocalAddress())) {
                    addressList.add(adr);
                }
            }
        }
        return addressList;
    }
}

Related

  1. getLocalIPAddresses()
  2. getLocalIPAddresses()
  3. getLocalIpAddresses()
  4. getLocalIpByInterfaceName(String interfaceName)
  5. getLocalIPByNetworkInterface()
  6. getLocalIPList()
  7. getLocalIps()
  8. getLocalIPs()
  9. getLocalIPs()