集册 Java实例教程 列出网络接口

列出网络接口

欢马劈雪     最近更新时间:2020-01-02 10:19:05

399
列出网络接口
/**
N o w J a v a . c o m - 时代Java
**/

import java.io.*;

import java.net.*;

import java.util.*;

import static java.lang.System.out;


public class ListNets {


    public static void main(String args[]) throws SocketException {

        Enumeration<NetworkInterface> nets = NetworkInterface

                .getNetworkInterfaces();

        for (NetworkInterface netint : Collections.list(nets))

            displayInterfaceInformation(netint);

    }


    static void displayInterfaceInformation(NetworkInterface netint)

            throws SocketException {

        out.printf("Display name: %s\n", netint.getDisplayName());

        out.printf("Name: %s\n", netint.getName());

        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();

        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        /** 
        来 自 
        N o  w  J a v a . c o m - 时  代  Java
        **/

            out.printf("InetAddress: %s\n", inetAddress);

        }

        out.printf("\n");

    }

}


展开阅读全文