get Inet Address By List - Java Network

Java examples for Network:Network Address

Description

get Inet Address By List

Demo Code


//package com.java2s;
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 {
    public static void main(String[] argv) throws Exception {
        System.out.println(getInetAddressByList());
    }/*from  ww w . ja  v a  2 s.c om*/

    public static List<InetAddress> getInetAddressByList()
            throws SocketException {
        List<InetAddress> list = new ArrayList<InetAddress>();
        Enumeration<NetworkInterface> enumeration = NetworkInterface
                .getNetworkInterfaces();
        while (enumeration.hasMoreElements()) {
            NetworkInterface networkInterface = enumeration.nextElement();
            Enumeration<InetAddress> enumerationInetAddress = networkInterface
                    .getInetAddresses();
            while (enumerationInetAddress.hasMoreElements()) {
                list.add(enumerationInetAddress.nextElement());
            }
        }
        return list;
    }
}

Related Tutorials