查询IP

package Demo01;

import java.net.InetAddress;

//测试IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress InetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println( InetAddress1); ///127.0.0.1
InetAddress InetAddress3 = InetAddress.getByName("localhost");
System.out.println( InetAddress3); //localhost/127.0.0.1
InetAddress InetAddress4 = InetAddress.getLocalHost();
System.out.println( InetAddress4); //DESKTOP-PQLGR9D/192.168.177.1

//查询网站ip地址
InetAddress InetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println( InetAddress2); //www.baidu.com/183.232.231.174

System.out.println("=============================================");

//常用方法
System.out.println(InetAddress2.getAddress()); //[B@131245a
System.out.println(InetAddress2.getCanonicalHostName());//规范的名字 183.232.231.174
System.out.println(InetAddress2.getHostAddress());//ip 183.232.231.174
System.out.println(InetAddress2.getHostName());//域名,或者本机电脑的名字 www.baidu.com
} catch (Exception e) {
e.printStackTrace();
}
}
}