简体   繁体   中英

how to understand NetworkInterface and InetAddress concept?

I think in java, NetworkInterface is abstraction of network card . If this is right , why one NetworkInterface contains more than one InetAddress ?

for example this code in my macbook :

NetworkInterface en0 = NetworkInterface.getByName("en0");
Enumeration<InetAddress> adrs = en0.getInetAddresses() ;
while(adrs.hasMoreElements()){
    InetAddress adr = adrs.nextElement();
    System.out.println(adr.getHostAddress());
}

Output:

fe80:0:0:0:7a31:c1ff:feb7:a3a2%4
192.168.1.100

There are two address, but I didn't use IPv6.

My question is that, how to understand this two concepts? What's their relationship?

why one NetworkInterface contains more than one InetAddress?

Because you can configure an NIC with multiple addresses. This has been the case for about twenty years.

I didn't use IPv6

Somebody did. Maybe you're using DHCP and it came from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM