码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA NetworkInterface类静态方法

时间:2014-11-21 12:12:39      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   on   

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class networkInterfaceStaticMethod {

    public static void main(String[] args) {
        
        networkInterfaceStaticMethod.interface_getByIndex(0);
        
        networkInterfaceStaticMethod.interface_getByInetAddress("10.11.3.160");
        
        networkInterfaceStaticMethod.interface_getNetworkInterfaces();
    }
    
    
    public static void interface_getByIndex(int index) {
        
        NetworkInterface ni = null;
        try {
            ni = NetworkInterface.getByIndex(index);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        
        System.out.println((ni == null) ? "interface not exist!" : ni.getName() + " : " + ni.getDisplayName() + " : " + ni.getIndex());
    }
    
    
    public static void interface_getByInetAddress(String host) {
        
        InetAddress local;
        NetworkInterface ni = null;
        try {
            local = InetAddress.getByName(host);
            ni = NetworkInterface.getByInetAddress(local);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        Enumeration<InetAddress> addresses = ni.getInetAddresses();
        while(addresses.hasMoreElements()) {
            System.out.println(addresses.nextElement().getHostAddress());
        }
    }
    
    
    public static void interface_getNetworkInterfaces() {
        
        Enumeration<NetworkInterface> nis = null;
        try {
            nis = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        while(nis.hasMoreElements()) {
            System.out.println(nis.nextElement());
        }
    }
}

 

JAVA NetworkInterface类静态方法

标签:style   blog   io   ar   color   os   sp   java   on   

原文地址:http://www.cnblogs.com/starzou/p/4112373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!