Java InetAddress Create getActiveInterfaceInetAddresses()

Here you can find the source of getActiveInterfaceInetAddresses()

Description

get Active Interface Inet Addresses

License

Apache License

Declaration

public static Vector<InetAddress> getActiveInterfaceInetAddresses() 

Method Source Code

//package com.java2s;
/**/*from  w ww  .  ja  v a  2 s  .  c  o  m*/
 * Mobility First - Global Name Resolution Service (GNS)
 * Copyright (C) 2013 University of Massachusetts - Emmanuel Cecchet.
 * Contact: cecchet@cs.umass.edu
 * 
 * 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. 
 *
 * Initial developer(s): Emmanuel Cecchet.
 * Contributor(s): ______________________.
 */

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Vector;

public class Main {
    /**
     *
     * @return
     */
    public static Vector<InetAddress> getActiveInterfaceInetAddresses() {
        Vector<InetAddress> CurrentInterfaceIPs = new Vector<InetAddress>();
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                    .hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        // FIXME: find better method to get ipv4 address
                        String IP = inetAddress.getHostAddress();
                        if (IP.contains(":")) // means IPv6
                        {
                            continue;
                        } else {
                            CurrentInterfaceIPs.add(inetAddress);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return CurrentInterfaceIPs;
    }
}

Related

  1. getAddressInfoFindAll(Function convertValue, Predicate doBreak)
  2. getAddressPriority(InetAddress addr)
  3. getAddrInfo(InetAddress pAddr)
  4. getAllInetAddress(final String interfaceName)