首頁  >  文章  >  Java  >  Java Inet位址

Java Inet位址

PHPz
PHPz原創
2024-08-30 15:39:39860瀏覽

java InetAddress 用來指定 IP 位址。 IP 位址是指分配給網路中機器的唯一數位標籤。對於 IPv4,IP 位址指定為 32 位元;對於 IPv6 位址,指定為 128 位元。 InetAddress 的執行個體會根據建立期間是否執行主機名稱解析來指定作為主機名稱的 IP 位址。位址有兩種類型:單播和組播。單一介面由單播位址標識,一組介面由多播位址標識。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

java中的InetAddress類別內建於java的java.net.InetAddress套件中。

InetAddress 類別可用於取得任何主機的 IP 位址,例如 www.educba.com、www.google.com 等。常用的 IP 位址是 IPv4,即「版本 4」。考慮一個 IP 位址的範例,它可能類似於 –

65.172.248.170

上面的位址包含四個數字,每個數字由三個數字組成,並以「.」(單點)分隔。這四個數字的範圍是 0 到 255。

InetAddress 類別成員函數

InetAddress 類別不包含任何建構函數,但包含一些函數作為 InetAddress 類別成員函數。

成員Java 函數InetAddress 類別

類別 –
  1. public static InetAddressgetByAddress(String host, byte[] addr) throws UnknownHostException – 此靜態函數傳回 InetAddress 的對象,其中包含主機名稱和 IP 位址作為傳遞的參數。 host 參數可以是字串格式的 IP 位址,也可以是機器名稱,例如 www.educba.com。
  2. public static InetAddressgetByName(String host) – 此靜態函數傳回指定主機的 InetAddress 物件。其中參數host為指定主機名稱。
  3. public static InetAddress[] getAllByName(String host) – 此靜態函數傳回指定主機的 InetAddress 物件陣列。
  4. public static InetAddressgetLoopbackAddress() – 此靜態函數傳回 Loopback 類型的 InetAddress 物件。
  5. public static InetAddressgetLocalHost() throws UnknownHostException – 此靜態函數傳回本機的 InetAddress 物件。
  6. public byte[] getAddress() – 此函數以陣列形式傳回 InetAddress 物件的 IP 位址。數組儲存 IP 位址會以位元組順序顯示,如 IP 位址中所示。
  7. public String getHostAddress() – 此函數以字串格式傳回 IP 位址。
  8. public boolean isReachable(int timeout)throws IOException – 如果 IP 位址可達,則此函數傳回 true,而 timeout 參數指定呼叫中止後的時間,並導致 false值。
  9. public boolean isReachable(NetworkInterfacenetif, intttl, int timeout) throws IOException – 此函式重載了 isReachable() 函式。其中 netif 表示用於檢查可達性的網路接口,ttl 表示退出網路之前 echo 封包所經過的跳數,並且由於 timeout 參數指定了此後的時間,因此呼叫將被中止。
  10. public String getHostName() – 此函數傳回 IP 位址的主機名稱。
  11. public String getCanonicalHostName() – 此函數傳回 IP 位址的完全限定網域名稱。
  12. public String toString() –此函數表示字串格式的 IP 位址。
  13. public boolean isAnyLocalAddress() – 如果 InetAddress 物件位址是本機位址,則此函數傳回 true。
  14. public boolean isLinkLocalAddress() – 如果 InetAddress 物件位址是連結本機位址,則此函數傳回 true。
  15. public boolean isLoopbackAddress() – 如果 InetAddress 物件位址是環回位址,則此函數傳回 true。
  16. public boolean isMCGLoabal() – 如果 IP 多播位址具有全域範圍,則此函數傳回 true。
  17. public boolean isMCLinkLocal() – 如果 IP 多播位址具有連結範圍,則此函數傳回 true。
  18. public boolean isMCNodeLocal() – 如果 IP 多重播送位址具有節點範圍,則此函數傳回 true。
  19. public boolean isMCOrgLoacal() – 如果 IP 多播位址具有組織範圍,則此函數傳回 true。
  20. public boolean isMCSiteLocal() – 如果 IP 多播位址具有網站範圍,則此函數傳回 true。
  21. public boolean isMulticastAddress() – 如果 IP 位址是前四位為 1110 的多重播放位址,則此函數傳回 true。
  22. public boolean isSiteLocalAddress() – 如果 IP 位址是網站本機位址,則此函數傳回 true。
  23. public int hashCode() – 此函數傳回 IP 位址雜湊碼。
  24. public boolean equals(Object obj) – 如果 IP 位址與傳遞的 IP 位址相同,則此函數傳回 true。

java 中 InetAddress 類別的範例

接下來,我們編寫 java 程式碼來更清楚地理解 InetAddress 類,在下面的範例中,我們使用上面討論的 URL 和該物件中的一些函數來建立一個 InetAddress 物件 –

範例#1

代碼:

import java.io.IOException;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main( String[] arg) throws IOException
{
InetAddress ip = InetAddress.getByName("www.educba.com");
byte addr[] = { 65, 2, 0, 1};
System.out.print("iptoString : " + ip.toString());
System.out.print("\ngetAllByName : " + ip.getAllByName("www.educba.com"));
InetAddress ips[] = InetAddress.getAllByName("www.educba.com");
System.out.println("IP Address");
for (InetAddress add:ips)
System.out.println(add.getHostAddress());
// function getByName()
System.out.print("\ngetByName : " + ip);
// function getByAddress()
System.out.print("\ngetByAddress : " +InetAddress.getByAddress(addr));
// function getLocalHost()
System.out.print("\ngetLocalHost : " +InetAddress.getLocalHost());
// function getLoopbackAddress()
System.out.print("\ngetLoopbackAddress : " +InetAddress.getLoopbackAddress());
// function getAllByName() which returns all ip addresses of google.com
System.out.print("\nGoogleip addresses : " + Arrays.toString(InetAddress.getAllByName("www.google.com")));
// function isReachable()
System.out.print("\nip address isReachable : " +ip.isReachable(50));
// function getHostname()
System.out.print("\nip address hostname :" +ip.getHostName());
// function getCanonicalHostname()
System.out.print("\nip address CanonicalHostname : " + ip.getCanonicalHostName());
}
}

輸出:

Java Inet位址

接下來,我們為 InetAddress 類別編寫 java 程式碼,其中我們在 InetAddress 物件上應用剩餘的布林函數 –

範例#2

代碼:

import java.net.Inet4Address;
import java.util.Arrays;
import java.net.InetAddress;
public class Demo
{
public static void main(String[] arg) throws Exception
{
InetAddress ip =  Inet4Address.getByName("www.educba.com");
InetAddress ip1[] = InetAddress.getAllByName("www.educba.com");
byte addr[]={68, 5, 2, 12};
System.out.println("ip : "+ip);
System.out.print("\nip1 : "+ip1);
InetAddress ip2 =  InetAddress.getByAddress(addr);
System.out.print("\nip2 : "+ip2);
System.out.print("\nAddress : " +Arrays.toString(ip.getAddress()));
System.out.print("\nHost Address : " +ip.getHostAddress());
System.out.print("\nisAnyLocalAddress : " +ip.isAnyLocalAddress());
System.out.print("\nisLinkLocalAddress : " +ip.isLinkLocalAddress());
System.out.print("\nisLoopbackAddress : " +ip.isLoopbackAddress());
System.out.print("\nisMCGlobal : " +ip.isMCGlobal());
System.out.print("\nisMCLinkLocal : " +ip.isMCLinkLocal());
System.out.print("\nisMCNodeLocal : " +ip.isMCNodeLocal());
System.out.print("\nisMCOrgLocal : " +ip.isMCOrgLocal());
System.out.print("\nisMCSiteLocal : " +ip.isMCSiteLocal());
System.out.print("\nisMulticastAddress : " +ip.isMulticastAddress());
System.out.print("\nisSiteLocalAddress : " +ip.isSiteLocalAddress());
System.out.print("\nhashCode : " +ip.hashCode());
System.out.print("\n Is ip1 == ip2 : " +ip.equals(ip2));
}
}

輸出:

Java Inet位址

結論

InetAddress 是 java 中的內建類,可在 java.net.InetAddress 套件中使用。它用於指定網路中機器的IP位址。我們討論的上述方法可用於獲取有關 IP 位址的更多資訊。

以上是Java Inet位址的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 金鑰庫下一篇:Java 金鑰庫