當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java java.net.InetAddress用法及代碼示例


公開課InetAddress延伸對象實現可序列化:

java.net.InetAddress 類提供了獲取任何主機名的 IP 地址的方法。 IP地址由32位或128位無符號數表示。 InetAddress 可以處理 IPv4 和 IPv6 地址。

地址有 2 種類型:

  1. 單播 —單個接口的標識符。
  2. 組播 —一組接口的標識符。

InetAddress - 工廠方法:

InetAddress類用於封裝數字 IP 地址和該地址的域名。 InetAddress 類沒有可見的構造函數。 InetAddress 類無法直接創建對象,因此工廠方法用於該目的。工廠方法是類中返回該類的對象的靜態方法。

InetAddress 類中有 5 個工廠方法可用 -

方法 說明
公共靜態InetAddress getLocalHost()拋出UnknownHostException 此方法返回包含本地主機名和地址的 InetAddress 實例。
公共靜態InetAddress getByName(字符串主機)拋出UnknownHostException 此方法返回 InetAddress 的實例,其中包含由以下表示的主機的 IP 和主機名主持人爭論。
公共靜態 InetAddress[] getAllByName( String 主機名 )拋出UnknownHostException 此方法返回包含 IP 地址的 InetAddress 類實例的數組。
公共靜態 InetAddress getByAddress( 字節 IPAddress[] )拋出UnknownHostException 此方法返回從原始 IP 地址創建的 InetAddress 對象。
公共靜態 InetAddress getByAddress( String 主機名, byte IPAddress[] )拋出UnknownHostException 此方法根據提供的主機名和 IP 地址創建並返回 InetAddress。

下麵是InetAddress類的Java實現,演示工廠方法的使用 -

Java


import java.io.*;
import java.net.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
        // To get and print InetAddress of Local Host
        InetAddress address1 = InetAddress.getLocalHost();
        System.out.println("InetAddress of Local Host : "
                           + address1);
        // To get and print InetAddress of Named Host
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        System.out.println("InetAddress of Named Host : "
                           + address2);
        // To get and print ALL InetAddresses of Named Host
        InetAddress address3[]
            = InetAddress.getAllByName("172.19.25.29");
        for (int i = 0; i < address3.length; i++) {
            System.out.println(
                "ALL InetAddresses of Named Host : "
                + address3[i]);
        }
        // To get and print InetAddresses of
        // Host with specified IP Address
        byte IPAddress[] = { 125, 0, 0, 1 };
        InetAddress address4
            = InetAddress.getByAddress(IPAddress);
        System.out.println(
            "InetAddresses of Host with specified IP Address : "
            + address4);
        // To get and print InetAddresses of Host
        // with specified IP Address and hostname
        byte[] IPAddress2
            = { 105, 22, (byte)223, (byte)186 };
        InetAddress address5 = InetAddress.getByAddress(
            "gfg.com", IPAddress2);
        System.out.println(
            "InetAddresses of Host with specified IP Address and hostname : "
            + address5);
    }
}
輸出
InetAddress of Local Host : localhost/127.0.0.1
InetAddress of Named Host : /45.22.30.39
ALL InetAddresses of Named Host : /172.19.25.29
InetAddresses of Host with specified IP Address : /125.0.0.1
InetAddresses of Host with specified IP Address and hostname : gfg.com/105.22.223.186

InetAddress — 實例方法:

InetAddress 類有大量可以使用對象調用的實例方法。實例方法是 -

方法 說明
equals(Object obj) 此函數將此對象與指定對象進行比較。
getAddress() 此方法返回此 InetAddress 對象的原始 IP 地址(以字節為單位)。
getCanonicalHostName() 此方法返回此 IP 地址的完全限定域名。
getHostAddress() 該方法獲取字符串形式的IP地址。
getHostName() 此方法返回此 IP 地址的主機名。
hashCode() 此方法獲取此 IP 地址的哈希碼。
isAnyLocalAddress() 此方法實用程序檢查InetAddress是否是不可預測的地址。
isLinkLocalAddress() 此方法實用程序檢查該地址是否未鏈接到本地單播地址。
isLoopbackAddress() 該方法用於檢查InetAddress是否代表環回地址。
isMCGlobal() 該方法實用程序檢查該地址是否具有全局範圍的多播地址。
isMCLinkLocal() 該方法實用程序檢查該地址是否具有link-local範圍的多播地址。
isMCNodeLocal() 該方法實用程序檢查多播地址是否具有節點範圍。
isMCOrgLocal() 該方法實用程序檢查多播地址是否具有組織範圍。
isMCSiteLocal() 此方法實用程序檢查多播地址是否具有站點範圍。
isMulticastAddress() 此方法檢查站點是否有多個服務器。
isReachable(int 超時) 此方法測試該地址是否可達。
isReachable(網絡接口 netif, int ttl, int 超時) 此方法測試該地址是否可達。
isSiteLocalAddress() 該方法實用程序檢查InetAddress是否是site-local地址。
toString() 該方法轉換並返回字符串形式的IP地址。

下麵是 InetAddress 類的 Java 實現,演示實例方法的使用 -

Java


import java.io.*;
import java.net.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
        throws UnknownHostException
    {
        InetAddress address1
            = InetAddress.getByName("45.22.30.39");
        InetAddress address2
            = InetAddress.getByName("45.22.30.39");
        InetAddress address3
            = InetAddress.getByName("172.19.25.29");
        // true, as clearly seen above
        System.out.println(
            "Is Address-1 equals to Address-2? : "
            + address1.equals(address2));
        // false
        System.out.println(
            "Is Address-1 equals to Address-3? : "
            + address1.equals(address3));
        // returns IP address
        System.out.println("IP Address : "
                           + address1.getHostAddress());
        // returns host name,
        // which is same as IP
        // address in this case
        System.out.println(
            "Host Name for this IP Address : "
            + address1.getHostName());
        // returns address in bytes
        System.out.println("IP Address in bytes : "
                           + address1.getAddress());
        // false, as the given site
        //  has only one server
        System.out.println("Is this Address Multicast? : "
                           + address1.isMulticastAddress());
        System.out.println("Address in string form : "
                           + address1.toString());
        // returns fully qualified
        // domain name for this IP address.
        System.out.println(
            "Fully qualified domain name for this IP address : "
            + address1.getCanonicalHostName());
        // hashcode for this IP address.
        System.out.println("Hashcode for this IP address : "
                           + address1.hashCode());
        // to check if the InetAddress is
        // an unpredictable address..
        System.out.println(
            "Is the InetAddress an unpredictable address? : "
            + address1.isAnyLocalAddress());
    }
}
輸出
Is Address-1 equals to Address-2? : true
Is Address-1 equals to Address-3? : false
IP Address : 45.22.30.39
Host Name for this IP Address : 45.22.30.39
IP Address in bytes : [B@579bb367
Is this Address Multicast? : false
Address in string form : 45.22.30.39/45.22.30.39
Fully qualified domain name for this IP address : 45.22.30.39
Hashcode for this IP address : 756424231
Is the InetAddress an unpredictable address? : false


相關用法


注:本文由純淨天空篩選整理自vanshitatwr620大神的英文原創作品 java.net.InetAddress Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。