Java之網絡編程學習筆記二 —— IP

Java之網絡編程學習筆記二 —— IP


IP簡介

ip地址:對應java裏的 InetAddress 類

  • 唯一定位網絡上的一臺計算機
  • 127.0.0.1 本機localhost
  • ip地址分類
    • ip地址分類:IPV4和IPV6
      • IPV4 127.0.0.1, 4個字節組成,每個字節長度 0~255
      • IPV6 128位,8個無符號整數
    • 公網(互聯網)和私網(局域網)
      • ABCD類地址
      • 局域網 192.168.x.x 類似的,給組織單位使用的

在這裏插入圖片描述

  • 域名:解決IP難記憶的問題
    • www.xxx.com

相關代碼

package pers.ylw.lesson01;

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

//測試IP
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查詢本機地址
            InetAddress inetAddress01 = InetAddress.getByName("127.0.0.1");//返回/127.0.0.1
            System.out.println(inetAddress01);
            InetAddress inetAddress02 = InetAddress.getByName("localhost");//返回localhost/127.0.0.1
            System.out.println(inetAddress02);
            InetAddress inetAddress03 = InetAddress.getLocalHost(); //返回計算機名/192開頭ip
            System.out.println(inetAddress03);

            //查詢網站ip地址
            InetAddress inetAddress04 = InetAddress.getByName("www.baidu.com");//返回www.baidu.com/14.215.177.38
            System.out.println(inetAddress04);

            //常用方法
            //System.out.println(inetAddress04.getAddress()); //返回一組地址,沒什麼用
            System.out.println(inetAddress04.getCanonicalHostName()); //返回規範ip
            System.out.println(inetAddress04.getHostAddress()); //返回ip地址
            System.out.println(inetAddress04.getHostName()); //獲取域名,或計算機名

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章