Java IP Address Get getLocalIps()

Here you can find the source of getLocalIps()

Description

get Local Ips

License

Apache License

Exception

Parameter Description
SocketException an exception

Declaration

private static List<String> getLocalIps() throws SocketException 

Method Source Code

//package com.java2s;
/**/*from   w w w  .ja v  a  2s  .co m*/
 * Copyright 2013 openteach
 *
 *  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.Inet4Address;
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 {
    /**
     * 
     * @return
     * @throws SocketException 
     */
    private static List<String> getLocalIps() throws SocketException {
        List<String> ipList = new ArrayList<String>(2);
        NetworkInterface ni = null;
        Enumeration<InetAddress> addresses = null;
        InetAddress address = null;
        Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
        while (enumeration.hasMoreElements()) {
            ni = enumeration.nextElement();
            if (ni.isLoopback() || ni.isVirtual() || !ni.isUp()) {
                continue;
            }
            addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                address = addresses.nextElement();
                if (address instanceof Inet4Address) {
                    ipList.add(address.getHostAddress());
                }
            }
        }
        return ipList;
    }
}

Related

  1. getLocalIpAddresses()
  2. getLocalIpByInterfaceName(String interfaceName)
  3. getLocalIPByNetworkInterface()
  4. getLocalIPCollection()
  5. getLocalIPList()
  6. getLocalIPs()
  7. getLocalIPs()
  8. getLocalIPs(boolean refresh)
  9. getLocalIpv4()