03 IP地址
IP地址(Internet Protocol) IP协议中还有一个非常重要的内容,那就是给因特网上的每台计算机和其它设备都规定了一个唯一的地址,叫做“IP地址”。由于有这种唯一的地址,才保证了用户在连网的计算机上操作时,能够高效而且方便地从千千万万台计算机中选出自己所需的对象来。 它为互联网上的每一个网络和每一台主机分配一个逻辑地址,以此来屏蔽物理地址的差异。
package Internet;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.UnknownHostException;
public class Test03 {
public static void main(String[] args) throws UnknownHostException {
// 查询本机IP地址,三个方法
InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3=InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4=InetAddress.getLocalHost();
System.out.println(inetAddress4);
// 查询百度IP地址
InetAddress inetAddress2=InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getHostAddress());
System.out.println(inetAddress1.getHostName());
}
}

浙公网安备 33010602011771号