歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java網絡編程之查找Internet地址

Java網絡編程之查找Internet地址

日期:2017/3/1 9:27:21   编辑:Linux編程

閱讀目錄

  • 一、概述
  • 二、什麼是IP地址?
  • 三、InetAddress類

一、概述

  連接到Internet上計算機都有一個稱為Internet地址或IP地址的唯一的數來標識。由於IP很難記住,人們設計了域名系統(DNS),DNS可以將人們可以記憶的主機名與計算機可以記憶的IP地址聯系在一起。通常一台主機映射一個IP地址。有時一個主機名映射多個IP。這時就由DNS負責隨機選擇一台機器來響應請求,例如業務流量很大的Web網站,它將負載分到多個系統上。

二、什麼是IP地址?

  IP地址是IP使用的32為或128位無符號數字,它是一種低級協議,UDP和TCP協議都是在它的基礎上構建的。

  1、地址類型

  • 單播:單個接口的標識符,發送到單播地址的數據包被交付給由該地址標示的接口。
  • 多播:一組接口(通常屬於不同的節點)的標識符,發送到多播地址的數據包被交付給由該地址標示的所有接口。

  2、IP范圍

  • 鏈接本地:地址設計用於在單個鏈接上尋址以解決自動地址配置、鄰居發現或沒有路由器時的問題。
  • 站點本地:地址設計用於在不需要全局前綴時站點內部尋址。
  • 全局地址:在Internet中是唯一的。

  3、IP地址的文本表示形式

  IP地址的文本表示形式是特定於地址系列的。

  IPV4地址一般寫為四個無符號字節,每個字節范圍從0到255。這種方式又稱為點分四段格式。eg:192.168.1.119。

三、InetAddress類

  InetAddress類表示互聯網協議(IP)地址,是Java對IP地址的高級表示。用於其他大多數網絡類。

  InetAddress類的實例包含IP地址,還可能包含相應的主機名(取決於它是否用主機名構造或者是否已執行反向主機名解析)。

  1、構造InetAddress對象

  InetAddress類沒有公共構造函數,可以通過其靜態方法返回適當初始化的InetAddress對象。

static InetAddress[] getAllByName(String host) Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. static InetAddress getByName(String host) Determines the IP address of a host, given the host's name. static InetAddress getLocalHost()    Returns the address of the local host.


ps:需要只指出的是,這些方法不只是使用它們的參數來設置內部字段,還需要進行網絡連接來獲取所需的所有信息。這個類的其他方法則主要使用上述方法提供的信息來工作。

  由於DNS查找成本相對較高,InetAddress類緩存查找的結果,可以通過networkaddress.cache.ttl指定成功的DNS查找在Java緩存中保留的秒數。除了在InetAddress類中的本地化緩存,本地主機、本地域名服務器和Internet中其他地方的DNS服務器也會緩存各種查找結果。

  2、類方法

boolean equals(Object obj) Compares this object against the specified object. byte[] getAddress() Returns the raw IP address of this InetAddress object. static InetAddress[] getAllByName(String host) Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. static InetAddress getByAddress(byte[] addr) Returns an InetAddress object given the raw IP address . static InetAddress getByAddress(String host, byte[] addr) Creates an InetAddress based on the provided host name and IP address. static InetAddress getByName(String host) Determines the IP address of a host, given the host's name. String getCanonicalHostName() Gets the fully qualified domain name for this IP address. String getHostAddress() Returns the IP address string in textual presentation. String getHostName() Gets the host name for this IP address. static InetAddress getLocalHost() Returns the address of the local host. static InetAddress getLoopbackAddress() Returns the loopback address. int hashCode() Returns a hashcode for this IP address.

  3、InetAddress示例代碼

public class Demo1
{

public static void main(String[] args)
{
InetAddress ina;
try
{
ina = InetAddress.getLocalHost();
System.out.println(ina);

System.out.println(ina.getAddress());//返回此 InetAddress 對象的原始 IP地址

System.out.println(ina.getHostAddress());// 返回 IP 地址字符串(以文本表現形式)。

System.out.println(ina.getHostName()); //獲取此 IP 地址的主機名

System.out.println(ina.getLocalHost()); //返回本地主機
}
catch (UnknownHostException e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
InetAddress ina1;
try
{
ina1 = InetAddress.getByName("192.168.1.119");
System.out.println(ina1);
}
catch (UnknownHostException e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
InetAddress[] ina2;
try
{
ina2 = InetAddress.getAllByName("www.microsoft.com");
for(int i=0;i<ina2.length;i++)
System.out.println(ina2[i]);
} catch (UnknownHostException e)
{
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}
}

Copyright © Linux教程網 All Rights Reserved