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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。