java 通过NetworkInterface获取本机ip地址信息

Socket:一台机器的通讯节点称为socket,在java中,socket是java.net包里面的Socket,ServerSocket,DataGramSocket,MultiSocket的实例。
IP地址java中用java.net.InetAddress来表示。
java network address class包括:
InetAddress:ip地址或者域名,指向远程地址。如果域名解析不了,则出错。
           抽象类,有两个子类:Inet4Address和Inet6Address。这两个类智能通过InetAddress的静态方法获取,不能直接构造。包可见。
InetSocketAddress extends SocketAddress:ip socket地址,用{ip,port}或者{hostname,port}表示。也能够单独用port构造,表示本机ip地址,所有本机网络地址。
NetwordInterface:本机网络接口,由多个网络接口名称和对应的网络接口的ip地址列表构成。


网络主机有两个特殊的网络地址:
loopback:127.0.0.1  用来检测本机的host名称,或者检测本地ip地址是否可用

wildcard:0.0.0.0 绑定了本机所有的ip地址

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;


public class LocalIP {

	private static final void printIp() {

        try {
            for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
                NetworkInterface item = e.nextElement();

                System.out.println(item.toString());
                System.out.println(item.getMTU() + " " + item.isLoopback() + " " + item.isPointToPoint() + " " + item.isUp() + " " + item.isVirtual());

                for (InterfaceAddress address : item.getInterfaceAddresses()) {
                    if (address.getAddress() instanceof Inet4Address) {
                        Inet4Address inet4Address = (Inet4Address) address.getAddress();
                        System.out.println(inet4Address.getHostAddress());
                        System.out.println(inet4Address.isLinkLocalAddress() + " " + inet4Address.isLoopbackAddress() + " " + inet4Address.isMCGlobal() + " " + inet4Address.isMulticastAddress());
                    }
                }
            }
        } catch (IOException ex) {

        }
    }
	public static void main(String[] args) {
		printIp();
	}
}

name:lo (Software Loopback Interface 1)-1 true false true false
127.0.0.1
false true false false
name:net0 (WAN Miniport (SSTP))
-1 false false false false
name:net1 (WAN Miniport (L2TP))
-1 false false false false
name:net2 (WAN Miniport (PPTP))
-1 false false false false
name:ppp0 (WAN Miniport (PPPOE))
-1 false false false false
name:eth0 (WAN Miniport (IPv6))
-1 false false false false
name:eth1 (WAN Miniport (Network Monitor))
-1 false false false false
name:eth2 (WAN Miniport (IP))
-1 false false false false
name:ppp1 (RAS Async Adapter)
-1 false false false false
name:eth3 (Realtek RTL8168D/8111D ??? PCI-E ??×????? NIC (NDIS 6.20))
1500 false false false false
name:net3 (Intel(R) Centrino(R) Advanced-N 6200 AGN)
1500 false false true false
10.16.194.27
false false false false
name:net4 (Teredo Tunneling Pseudo-Interface)
1280 false true false false
name:net5 (Microsoft ISATAP Adapter)
1280 false true false false
name:eth4 (SINFOR SSL VPN CS Support System VNIC)
1400 false false false false
name:net6 (Bluetooth ?豸(RFCOMM Э?é TDI))
-1 false false false false
name:eth5 (Bluetooth ?豸(???????ò??)
1500 false false false false
name:eth6 (Array Networks SSL VPN Adapter)
-1 false false false false
name:net7 (WAN Miniport (IKEv2))
-1 false false false false
name:net8 (Microsoft ISATAP Adapter #5)
1280 false true false false
name:net9 (Microsoft ISATAP Adapter #3)
1280 false true false false
name:net10 (Microsoft ISATAP Adapter #2)
1280 false true false false
name:net11 (Microsoft ISATAP Adapter #4)
-1 false false false false
name:net12 (Intel(R) Centrino(R) Advanced-N 6200 AGN-QoS Packet Scheduler-0000)
-1 false false false false
name:net13 (Intel(R) Centrino(R) Advanced-N 6200 AGN-WFP LightWeight Filter-0000)
-1 false false false false
name:eth7 (Realtek RTL8168D/8111D ??? PCI-E ??×????? NIC (NDIS 6.20)-QoS Packet Scheduler-0000)
-1 false false false false
name:eth8 (Realtek RTL8168D/8111D ??? PCI-E ??×????? NIC (NDIS 6.20)-WFP LightWeight Filter-0000)
-1 false false false false
name:eth9 (WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000)
-1 false false false false
name:eth10 (WAN Miniport (IP)-QoS Packet Scheduler-0000)
-1 false false false false
name:eth11 (WAN Miniport (IPv6)-QoS Packet Scheduler-0000)
-1 false false false false
name:eth12 (SINFOR SSL VPN CS Support System VNIC-QoS Packet Scheduler-0000)
-1 false false false false
name:eth13 (SINFOR SSL VPN CS Support System VNIC-WFP LightWeight Filter-0000)
-1 false false false false


posted @ 2012-06-02 08:46  zhwj184  阅读(689)  评论(0编辑  收藏  举报