首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络协议 >

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

2012-09-02 
java 通过NetworkInterface获取本机ip地址信息Socket:一台机器的通讯节点称为socket,在java中,socket是jav

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

热点排行