IP地址是具有關於如何到達特定主機的信息的地址,其是具有2^32的地址空間的32位唯一地址號。 InetAddress 類是 IP 地址的表示。它既代表 32 位 IPv4 地址,也代表 128 位 IPv6 地址。它是 Inet6Address 和 Inet4Address 類的超類。此類的實例由 IP 地址和通常主機名組成,具體取決於創建期間是否執行主機名解析。
Note: There are no constructors for this class but static methods which return instances of InetAddress class for general use
InetAddress 類的方法
方法 | 執行的操作 |
---|---|
equals() | 如果此 IP 地址與指定對象的 IP 地址相同,則返回 true。 Equals()方法在比較時不考慮主機名,隻考慮關聯的IP地址。 |
getAddress() | 以數組形式返回此 InetAddress 對象的原始 IP 地址。字節在數組中出現的順序與 IP 地址中的順序相同,即 getAddress[0] 將包含最高位字節。 |
getByAddress() | 創建一個InetAddress對象。它以主機名和 IP 地址作為參數。主機名可以是 “www.geeksforgeeks.org” 中的計算機名稱或其文本 IP 地址。 |
getByName() | 返回指定主機的 IP 地址。如果主機是文字 IP 地址,則僅檢查其有效性。 |
getAllByName() | 返回給定主機的 IP 地址數組 |
getLoopbackAddress() | 返回環回地址 |
getHostAddress() | 以文本形式返回 IP 地址。 |
getHostName() | 返回此 IP 地址的主機名。如果該對象是使用主機名創建的,則返回該對象,否則,執行反向查找以返回係統配置的主機名。 |
getLocalHost() | 返回本地主機的 IP 地址。 |
getCanonicalHostName() | 返回此對象的完全限定域名。如果該對象是使用主機名創建的,則返回該對象,否則,執行反向查找以返回係統配置的主機名。 |
hashCode() | 返回與該地址對象關聯的哈希碼。 |
isAnyLocalAddress() | 如果該地址代表本地地址,則返回 true。 |
isLinkLocalAddress() | 如果此地址是 link-local 地址,則返回 true。 |
isLoopbackAddress() | 如果該地址是環回地址,則返回 true。 |
isMCGlobal() | 如果此多播地址具有全局範圍,則返回 true。 |
isMCLinkLocal() | 如果此多播地址具有鏈路範圍,則返回 true。 |
isMCNodeLocal() | 如果此多播地址具有節點範圍,則返回 true。 |
isMCOrgLocal() | 如果此多播地址具有組織範圍,則返回 true。 |
isMCSiteLocal() | 如果此多播地址具有站點範圍,則返回 true。 |
isMulticastAddress() | 如果該地址是 IP 多播地址,則返回 true。多播地址的前 4 位為 1110。 |
isReachable() | 如果該地址可達,則返回 true。如果可以授予許可,則使用 ICMP 回顯請求,否則主機會嘗試在目標的端口 7 建立 TCP 連接。該方法一般作為各種程序中的前置條件使用,避免以後出現Host Unreachable異常 |
isReachable() | 指定檢查可達性時要使用的網絡接口,ttl 參數指定 echo 數據包在退出網絡之前所經過的跳數。 |
isSiteLocalAddress() | 如果此地址是 site-local 地址,則返回 true。 |
toString() | 將 IP 地址轉換為字符串。它將結果作為主機名/IP 地址返回。 |
例子:
Java
// Java Program to Illustrate Methods of Inetaddress Class
// Importing required classes
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws UnknownHostException
{
// Input URL
String url = "www.geeksforgeeks.org";
byte addr[] = { 127, 0, 0, 1 };
InetAddress ip1 = Inet4Address.getByName(url);
InetAddress ip2 = InetAddress.getByAddress(addr);
// Following methods checks the property
// of the thus created object
// getAddress() method
System.out.println(
"Address : "
+ Arrays.toString(ip1.getAddress()));
// getHostAddress() method
System.out.println("Host Address : "
+ ip1.getHostAddress());
// isAnyLocalAddress() method
System.out.println("isAnyLocalAddress : "
+ ip1.isAnyLocalAddress());
// isLinkLocalAddress() method
System.out.println("isLinkLocalAddress : "
+ ip1.isLinkLocalAddress());
// isLoopbackAddress() method
System.out.println("isLoopbackAddress : "
+ ip1.isLoopbackAddress());
// isMCGlobal() method
System.out.println("isMCGlobal : "
+ ip1.isMCGlobal());
// isMCLinkLocal() method
System.out.println("isMCLinkLocal : "
+ ip1.isMCLinkLocal());
// isMCNodeLocal() method
System.out.println("isMCNodeLocal : "
+ ip1.isMCNodeLocal());
// isMCOrgLocal() method
System.out.println("isMCOrgLocal : "
+ ip1.isMCOrgLocal());
// isMCSiteLocal() method
System.out.println("isMCSiteLocal : "
+ ip1.isMCSiteLocal());
// isMulticastAddress() method
System.out.println("isMulticastAddress : "
+ ip1.isMulticastAddress());
// isSiteLocalAddress() method
System.out.println("isSiteLocalAddress : "
+ ip1.isSiteLocalAddress());
// hashCode() method
System.out.println("hashCode : " + ip1.hashCode());
// equals() method
System.out.println("ip1==ip2 : " + ip1.equals(ip2));
}
}
輸出:
示例 2:
Java
// Java Program to Illustrate Methods of Inetaddress Class
// Importing required classes
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
throws IOException
{
// Input sample URL
String url = "www.geeksforgeeks.org";
byte addr[] = { 127, 0, 0, 1 };
// getByName() method
InetAddress ip1 = InetAddress.getByName(url);
System.out.println("getByName() : " + ip1);
// getByAddress() method
InetAddress ip2 = InetAddress.getByAddress(addr);
System.out.println("getByAddress() : " + ip2);
// getLocalHost() method
InetAddress ip3 = InetAddress.getLocalHost();
System.out.println("getLocalHost() : " + ip3);
// getLoopbackAddress() method
InetAddress ip4 = InetAddress.getLoopbackAddress();
System.out.println("getLoopbackAddress() : " + ip4);
// getAllByName() method
// Returns all ip addresses associated with
// 'google.com'
InetAddress addrs[]
= InetAddress.getAllByName("www.google.com");
System.out.println("Google ip addresses : "
+ Arrays.toString(addrs));
// isReachable() method
boolean isreach = ip1.isReachable(50);
System.out.println("ip1 isReachable() : "
+ isreach);
// getHostname() method
String hostname = ip1.getHostName();
System.out.println("ip1 hostname :" + hostname);
// getCanonicalHostname() method
System.out.println("ip1 CanonicalHostname : "
+ ip1.getCanonicalHostName());
// toString() method
System.out.println("ip1 toString() : "
+ ip1.toString());
}
}
輸出:
實現:以下程序使用InetAddress類來獲取給定域名的IP地址。當程序在連接到 Internet 的係統上運行時,它會給出給定域的 IP 地址。
例子:
Java
// Java program to Demonstrate Working of InetAddress Class
// by Finding IP address for a Domain Name
// Importing required classes
import java.net.*;
// Main class
// GetIPAddress
public class GFG {
// Main driver method
public static void main(String args[]) throws Exception
{
// Input sample URL
String url = "www.google.com";
// Try block to check for exceptions
try {
// Getting IP addresses related to the domain
InetAddress ips[]
= InetAddress.getAllByName(url);
// Displaying IP addresses
System.out.println("IP Address(es)");
for (InetAddress addr : ips)
System.out.println(addr.getHostAddress());
}
// Catch block to handle exceptions
catch (Exception ex) {
// Display message if exception occurs
System.out.println("host not found");
}
}
}
輸出:
相關用法
- Java Integer divideUnsigned()用法及代碼示例
- Java Integer equals()用法及代碼示例
- Java Integer getInteger()用法及代碼示例
- Java Integer longValue()用法及代碼示例
- Java Integer max()用法及代碼示例
- Java Integer min()用法及代碼示例
- Java Integer numberOfLeadingZeros()用法及代碼示例
- Java Integer numberOfTrailingZeros()用法及代碼示例
- Java Integer parseInt()用法及代碼示例
- Java Integer parseUnsignedInt()用法及代碼示例
- Java Integer remainderUnsigned()用法及代碼示例
- Java Integer reverseBytes(int i)用法及代碼示例
- Java Integer toBinaryString()用法及代碼示例
- Java Integer toHexString()用法及代碼示例
- Java Integer toUnsignedLong()用法及代碼示例
- Java Integer toUnsignedString()用法及代碼示例
- Java InputStream available()用法及代碼示例
- Java InputStream close()用法及代碼示例
- Java InputStream mark()用法及代碼示例
- Java InputStream markSupported()用法及代碼示例
- Java InputStream reset()用法及代碼示例
- Java InputStream skip()用法及代碼示例
- Java InputStreamReader close()用法及代碼示例
- Java InputStreamReader getEncoding()用法及代碼示例
- Java InputStreamReader ready()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 InetAddress Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。