import java.net.Inet4Address;import java.net.Inet6Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.util.Enumeration;/** * 通訊端學習 * * @author Administrator * */public class InteAddressExample {public static void main(String[] args) {// 活的主機的網路介面以及相關的地址資訊try {Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();if (interfaceList == null) {System.out.println("沒有介面被發現");} else {while (interfaceList.hasMoreElements()) {NetworkInterface iface = interfaceList.nextElement();//getName()返回本地介面。介面的本地名稱通常由字母和數位聯合組成System.out.println("介面名稱:" + iface.getName() + ":");Enumeration<InetAddress> addrList = iface.getInetAddresses();if (!addrList.hasMoreElements()) {System.out.println("\t(該介面下沒有找到對應的地址)");}while (addrList.hasMoreElements()) {InetAddress address = addrList.nextElement();System.out.println("\tAddress "+ ((address instanceof Inet4Address ? "(v4)": (address instanceof Inet6Address ? "(v6)": "(?)"))));System.out.println(": " + address.getHostAddress());}}}} catch (Exception e) {e.printStackTrace();}//擷取命令列輸入的每個參數所對應的主機名稱和地址for (String host:args){try {System.out.println(host + ":");InetAddress[] addressList = InetAddress.getAllByName(host);for (InetAddress address:addressList){System.out.println("\t" + address.getHostAddress() + "/" + address.getHostAddress());}} catch (Exception e) {e.printStackTrace();}}}}