网络编程-IP-2022-12-14

IP 唯一定位网络上一台计算机

127.0.0.1    本机 local Host  

IP地址的分类

  1、IPV4  127.0.0.1 由四个字节组成 0-255   42亿个  30亿在北美  4亿 亚洲   2011用尽

        IPV6  2409:8a20:e3c:43e0:4524:c9fb:bfec:77a1     128位  8个无符号整数

 

    2、公网(互联网)、私网 (局域网)

         192.168.XX.XX       局域网,给组织内部使用的

域名:记忆IP问题  www.vip.com 

   

package com.lr;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class webTest {
public static void main(String[] args) {
// Ctrl+Alt+T
try {
//查询本机地址
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("--------------------------------------------------------");

System.out.println(inetAddress2.getAddress());
System.out.println(inetAddress2.getHostAddress()); //IP
System.out.println(inetAddress2.getCanonicalHostName());
System.out.println(inetAddress2.getHostName()); // 域名或者自己电脑名字

} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}

 

posted @ 2022-12-14 20:05  Rui2022  阅读(17)  评论(0编辑  收藏  举报