public class InetAddress extends Object implements Serializable
IP地址是一个32位或128位无符号数使用的IP,一个低层协议赖以建立的协议如UDP和TCP。IP地址的结构是由RFC 790: Assigned Numbers, RFC 1918: Address Allocation for Private Internets,RFC 2365: Administratively Scoped IP Multicast定义,并RFC 2373: IP Version 6 Addressing Architecture。一个InetAddress实例由一个IP地址和可能的对应的主机名(取决于它是否是一个主机名或是否已经做了反向主机名解析构造)。
unicast An identifier for a single interface. A packet sent to a unicast address is delivered to the interface identified by that address. The Unspecified Address -- Also called anylocal or wildcard address. It must never be assigned to any node. It indicates the absence of an address. One example of its use is as the target of bind, which allows a server to accept a client connection on any interface, in case the server host has multiple interfaces.
The unspecified address must not be used as the destination address of an IP packet.
The Loopback Addresses -- This is the address assigned to the loopback interface. Anything sent to this IP address loops around and becomes IP input on the local host. This address is often used when testing a client.
multicast An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address.
本地连接地址被设计用于在一个链接的目的,如自动地址配置、邻居发现解决,或在没有路由器的存在。
本地站点地址被设计用来解决在一个不需要全局前缀的站点。
全球的地址的唯一的在互联网上。
IPv4地址的格式,请参阅Inet4Address#format;IPv6地址格式,请参阅Inet6Address#format。
这是影响了IPv4和IPv6的地址是用一个couple of System Properties。
反向域名解析意味着任何IP地址,与IP地址相关联的主机返回。
InetAddress类提供的方法来将主机名解析为IP地址,反之亦然。
默认情况下,当一个安全管理器安装,以防止DNS欺骗攻击,正面的主机名的决议结果缓存永远。当一个安全管理器没有安装时,默认的行为是为一个有限(实现依赖)的时间段缓存条目。失败的主机名称解析的结果是缓存一个很短的时间(10秒),以提高性能。
如果默认的行为是不需要的,然后一个java安全属性可以设置为不同的生存时间(TTL)积极的缓存值。同样,一个系统管理员可以配置不同的负缓存TTL值时。
两个java安全性能控制用于正面和负面的主机名解析缓存TTL值:
- networkaddress.cache.ttl
- Indicates the caching policy for successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the successful lookup. The default setting is to cache for an implementation specific period of time.
A value of -1 indicates "cache forever".
- networkaddress.cache.negative.ttl (default: 10)
- Indicates the caching policy for un-successful name lookups from the name service. The value is specified as as integer to indicate the number of seconds to cache the failure for un-successful lookups.
A value of 0 indicates "never cache". A value of -1 indicates "cache forever".
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
将此对象与指定的对象进行比较。
|
byte[] |
getAddress()
返回该对象的
InetAddress 原IP地址。
|
static InetAddress[] |
getAllByName(String host)
给定一个主机的名称,返回其IP地址的数组,基于系统上的配置的名称服务。
|
static InetAddress |
getByAddress(byte[] addr)
返回给定的原始IP地址
InetAddress 对象。
|
static InetAddress |
getByAddress(String host, byte[] addr)
它创造了一个基于所提供的主机名和IP地址。
|
static InetAddress |
getByName(String host)
确定主机的IP地址,给定主机名。
|
String |
getCanonicalHostName()
获取此IP地址的完全限定的域名。
|
String |
getHostAddress()
返回文本表示中的IP地址字符串。
|
String |
getHostName()
获取此IP地址的主机名。
|
static InetAddress |
getLocalHost()
返回本地主机的地址。
|
static InetAddress |
getLoopbackAddress()
返回回送地址。
|
int |
hashCode()
返回该IP地址的hash码。
|
boolean |
isAnyLocalAddress()
实用程序来检查是否一个通配符解决它。
|
boolean |
isLinkLocalAddress()
实用程序检查,如果它是一个链路本地地址。
|
boolean |
isLoopbackAddress()
实用程序来检查它是环回地址。
|
boolean |
isMCGlobal()
实用程序,检查多播地址是否具有全局范围。
|
boolean |
isMCLinkLocal()
实用程序,检查多播地址是否有连接范围。
|
boolean |
isMCNodeLocal()
实用程序,检查多播地址是否有节点范围。
|
boolean |
isMCOrgLocal()
实用程序,检查多播地址是否有组织范围。
|
boolean |
isMCSiteLocal()
实用程序,检查多播地址是否有网站范围。
|
boolean |
isMulticastAddress()
实用程序检查,如果它是一个IP多播地址。
|
boolean |
isReachable(int timeout)
测试是否该地址是可到达的。
|
boolean |
isReachable(NetworkInterface netif, int ttl, int timeout)
测试是否该地址是可到达的。
|
boolean |
isSiteLocalAddress()
实用程序检查,如果它是一个站点本地地址。
|
String |
toString()
将这个IP地址到一个
String 。
|
public boolean isMulticastAddress()
boolean
表示如果它是一个IP多播地址
public boolean isAnyLocalAddress()
boolean
表示如果它是一个通配符地址。
public boolean isLoopbackAddress()
boolean
表示如果它是环回地址;否则。
public boolean isLinkLocalAddress()
boolean
表示如果它是一个链路本地地址;或假如果地址不是链路本地单播地址。
public boolean isSiteLocalAddress()
boolean
表示如果它是一个站点本地地址;或假如果地址不是站点本地单播地址。
public boolean isMCGlobal()
boolean
表示如果地址已经是全球范围的组播地址,假如果不是全球范围或它不是一个多播地址
public boolean isMCNodeLocal()
boolean
表示如果地址是节点局部范围的组播地址,假如果不是节点局部范围或它不是一个多播地址
public boolean isMCLinkLocal()
boolean
表示如果地址是链路本地范围的组播地址,假如果不是链路本地范围或它不是一个多播地址
public boolean isMCSiteLocal()
boolean
表示如果地址是本地站点范围的组播地址,假如果不是局部范围或它不是一个多播地址
public boolean isMCOrgLocal()
boolean
表示如果地址是组织局部范围的组播地址,假如果不是组织局部范围或它不是一个多播地址
public boolean isReachable(int timeout) throws IOException
超时值,以毫秒为单位表示,应采取的尝试所需的最大时间。如果操作超时之前得到答案,主持人被认为是遥不可及的。负值将导致时抛出。
timeout
-时间,以毫秒为单位,在打电话时
boolean
指示如果地址不可达。
IOException
-如果网络发生错误
IllegalArgumentException
-如果
timeout
是负的。
public boolean isReachable(NetworkInterface netif, int ttl, int timeout) throws IOException
的network interface
和ttl
参数让调用者指定网络接口的测试将通过和最大跳数的数据包应该通过。为ttl
负值将导致时抛出。
超时值,以毫秒为单位表示,应采取的尝试所需的最大时间。如果操作超时之前得到答案,主持人被认为是遥不可及的。负值将导致时抛出。
netif
-网络接口通过测试将完成,或为任何接口
ttl
-最大的跳数尝试或0为默认
timeout
-时间,以毫秒为单位,在打电话时
boolean
indicating如果地址不可达。
IllegalArgumentException
-如果
timeout
或
ttl
负。
IOException
-如果网络发生错误
public String getHostName()
如果它是主机名创建,该主机名称将被返回;否则,反向名字查找将被执行并将结果返回给系统的基础上配置的名称查找服务。如果查找名字的服务是必需的,叫getCanonicalHostName
。
如果存在安全管理器,它的checkConnect
方法与主机名和-1
作为它的参数来看看操作允许先叫。如果操作是不允许的,它将返回IP地址的文本表示形式。
getCanonicalHostName()
,
SecurityManager.checkConnect(java.lang.String, int)
public String getCanonicalHostName()
如果存在安全管理器,该方法首先调用主机名和-1
作为它的参数是否调用代码可以知道这个IP地址的主机名,即其checkConnect
方法连接到主机。如果操作是不允许的,它将返回IP地址的文本表示形式。
SecurityManager.checkConnect(java.lang.String, int)
public byte[] getAddress()
InetAddress
原IP地址。其结果是在网络字节顺序:地址的字节是在
getAddress()[0]
。
public String getHostAddress()
public int hashCode()
hashCode
方法重写,继承类
Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public boolean equals(Object obj)
true
如果只有参数不
null
它代表相同的IP地址作为该对象。
对InetAddress
两实例表示相同的IP地址,如果getAddress
返回的字节数组的长度是相同的,并且每个数组组件是相同的字节数组。
equals
方法重写,继承类
Object
obj
-比较的对象。
true
如果对象是相同的;
false
否则。
getAddress()
public String toString()
String
。返回的字符串的形式是:主机名/文字的IP地址。如果主机名未得到解决,则不执行反向名称服务查找。主机部分由一个空字符串表示。
public static InetAddress getByAddress(String host, byte[] addr) throws UnknownHostException
主机名可以是机器的名称,如“java.sun.com
”,或考证其IP地址的表示。
没有对主机名称进行有效性检查。
如果指定一个IPv4地址inet4address实例将被返回;否则,将返回一个实例对Inet6Address。
IPv4地址的字节数组必须是4字节长,IPv6的字节数组必须是16字节长
host
-指定的主机
addr
-网络字节顺序的原始IP地址
UnknownHostException
如果IP地址是非法的长度
public static InetAddress getByName(String host) throws UnknownHostException
主机名可以是机器的名称,如“java.sun.com
”,或考证其IP地址的表示。如果提供了一个字面上的IP地址,则检查地址格式的有效性。
在文字的IPv6地址指定的host
,的形式在RFC 2732和RFC 2373中定义的字面的IPv6地址格式定义是可以接受的。IPv6组播地址也支持。看到一个描述here IPv6组播地址。
如果主机是null
然后代表环回接口地址InetAddress
返回。看到RFC 3330部分 2和RFC 2373部分 2.5.3。
host
-指定的主机,或
null
。
UnknownHostException
如果为
host
没有IP地址可以发现,如果一个scope_id是全球IPv6地址指定。
SecurityException
-如果存在一个安全管理及其checkconnect方法不允许操作
public static InetAddress[] getAllByName(String host) throws UnknownHostException
主机名可以是机器的名称,如“java.sun.com
”,或考证其IP地址的表示。如果提供了一个字面上的IP地址,则检查地址格式的有效性。
在文字的IPv6地址指定host
,要么在RFC 2732和RFC 2373中定义的字面的IPv6地址格式定义是可以接受的。字面上的IPv6地址,也可以通过添加一个域区标识符或scope_id合格。语法和用法scope_ids描述here。
如果主机是null
然后代表环回接口地址InetAddress
返回。看到RFC 3330部分 2和RFC 2373部分 2.5.3。
如果有一个安全管理和host
不是零和host.length()
并不等于零,安全经理的checkConnect
方法被称为主机名作为参数,-1
看看操作是允许的。
host
-主机的名称,或
null
。
UnknownHostException
如果为
host
没有IP地址可以发现,如果一个scope_id是全球IPv6地址指定。
SecurityException
-如果存在一个安全管理及其
checkConnect
方法不允许操作。
SecurityManager.checkConnect(java.lang.String, int)
public static InetAddress getLoopbackAddress()
但是它还将代表回送地址127.0.0.1,IPv4或IPv6,环回地址,::1。IPv4的回环地址返回只有一个表单中的127多个。* * *。
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
InetAddress
对象。这个论点是网络字节顺序:地址的字节是在
getAddress()[0]
。
此方法不阻塞,即不执行反向名称服务查找。
IPv4地址的字节数组必须是4字节长,IPv6的字节数组必须是16字节长
addr
-网络字节顺序的原始IP地址
UnknownHostException
如果IP地址是非法的长度
public static InetAddress getLocalHost() throws UnknownHostException
InetAddress
。
注:解决的地址可能会被缓存一个很短的时间内。
如果存在安全管理器,它的checkConnect
方法调用本地主机名作为参数,-1
看看操作是允许的。如果操作是不允许的,代表一个InetAddress返回回送地址。
UnknownHostException
如果本地主机名无法解析为一个地址。
SecurityManager.checkConnect(java.lang.String, int)
,
getByName(java.lang.String)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.