import java.io.*;
import java.net.*;
import java.util.*;

/**
 * An application that displays all of the network addresses
 * on this computer.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DisplayAddresses
{
    /**
     * The entry point of the application.
     *
     * @param args Command line arguments (ignored)
     */
    public static void main(String[] args)
    {
        Enumeration<NetworkInterface> nifs;

        try
        {
            display(NetworkInterface.getNetworkInterfaces());
        }
        catch (SocketException se)
        {
            System.out.println("Unable to get network interfaces!");
        }
    }

    /**
     * Display the addresses of an Enumeration of NetworkInterface objects.
     *
     * @param nifs   The Enumeration
     * @param indent The indentation String ("" for none and no recursion)
     */
    private static void display(Enumeration<NetworkInterface> nifs) 
                                throws SocketException
    {
        while (nifs.hasMoreElements())
        {
            NetworkInterface nif = nifs.nextElement();
            
            System.out.printf("%s (%s)\n", 
                              nif.getDisplayName(), nif.getName());

            Enumeration<InetAddress> addresses = nif.getInetAddresses();
            while (addresses.hasMoreElements())
            {
                System.out.printf("\t%s\n", addresses.nextElement());
            }
            System.out.println();
        }
    }
}
