Java IP Address Get getIpAddresses()

Here you can find the source of getIpAddresses()

Description

get Ip Addresses

License

Open Source License

Declaration

private static String[] getIpAddresses() throws SocketException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2007 Bug Labs, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.buglabs.net/legal/epl_license.html
 *******************************************************************************/

import java.net.Inet6Address;
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 {
    private static String[] getIpAddresses() throws SocketException {
        List addresses = new ArrayList();

        Enumeration e = NetworkInterface.getNetworkInterfaces();

        while (e.hasMoreElements()) {
            NetworkInterface netface = (NetworkInterface) e.nextElement();

            Enumeration e2 = netface.getInetAddresses();

            while (e2.hasMoreElements()) {
                InetAddress ip = (InetAddress) e2.nextElement();
                if (isValidIpAddress(ip)) {
                    addresses.add(ip.toString().replaceFirst("/", ""));
                }//from  w  w w  . j  av a2 s  .c  o  m
            }
        }

        return (String[]) addresses.toArray(new String[addresses.size()]);
    }

    /**
     * Determine if IP address is valid for BUG to use for event notification.
     * 
     * @param ip
     * @return
     */
    private static boolean isValidIpAddress(InetAddress ip) {
        if (ip instanceof Inet6Address) {
            return false;
        }

        // loopback
        if (ip.getAddress()[0] == 127) {
            return false;
        }

        return true;
    }
}

Related

  1. getIPAddress(NetworkInterface e)
  2. getIPAddress(String hostname)
  3. getIpAddress(String server)
  4. getIPAddresses()
  5. getIpAddresses()
  6. getIPAddresses()
  7. getIpAddressesFromNic()
  8. getIpAddressExternal()
  9. getIPAddressFromBytes(byte[] bytes)