集册 Java实例教程 返回此主机的所有网络接口。

返回此主机的所有网络接口。

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

417
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回此主机的所有网络接口。


//package com.nowjava;


import java.net.NetworkInterface;
/** from 
NowJava.com**/

import java.net.SocketException;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Enumeration;


public class Main {

    public static void main(String[] argv) throws Exception {

        System.out.println(getNetworkInterfaces());

    }


    /**

     * Returns all network interfaces of this host.

     * @return all network interfaces of this host

     */

    public static Collection<NetworkInterface> getNetworkInterfaces() {

        // get network interfaces

        ArrayList<NetworkInterface> result = new ArrayList<NetworkInterface>();

        Enumeration<NetworkInterface> ifs = null;
        /**
         * N o  w  J a v a . c o m - 时  代  Java 提 供 
        **/

        try {

            ifs = NetworkInterface.getNetworkInterfaces();

        } catch (SocketException e1) {

            throw new 
展开阅读全文