01-InetAddress常用方法
import java.net.InetAddress; import java.net.UnknownHostException; public class Demo01_初识IP { public static void main(String[] args) throws UnknownHostException { //获取本机IP地址 System.out.println(InetAddress.getLocalHost()); System.out.println(InetAddress.getByName("localhost")); System.out.println(InetAddress.getByName("127.0.0.1")); //按名称获取地址 InetAddress baidu = InetAddress.getByName("www.baidu.com"); System.out.println(InetAddress.getByName("www.zhangzhixi.com")); System.out.println(InetAddress.getByName("www.jd.com")); //常用方法 System.out.println(baidu.getAddress());//获取字节 System.out.println(baidu.getCanonicalHostName());//规范主机名 System.out.println(InetAddress.getByName("www.zhangzhixi.com").getCanonicalHostName()); System.out.println(baidu.getHostAddress());//主机IP System.out.println(baidu.getHostName());//域名 } }
zhixi/192.168.137.1
localhost/127.0.0.1
/127.0.0.1
www.zhangzhixi.com/3.223.115.185
www.jd.com/111.6.187.131
[B@74a14482
39.156.66.18
ec2-3-223-115-185.compute-1.amazonaws.com
39.156.66.18
www.baidu.com