此类表示IPv6地址,并扩展了InetAddress类。此类方法提供了表示和解释IPv6地址的工具。 此类的方法以以下格式获取输入:
null
- x:x:x:x:x:x:x:x:x:x- 这是IPv6地址的一般形式,其中每个x可以替换为地址的16位十六进制值。请注意,使用此格式时,每个“x”都必须有一个值。例如
4B0C:0:0:0:880C:99A8:4B0:4411
- 当地址包含多组8位为“0”时,可以使用特殊格式来压缩地址。在这种情况下,将用“:”代替0,以缩短地址。例如,前面示例中的地址可以写为-
4B0C::880C:99A8:4B0:4411
- x:x:x:x:x:x:d.d.d- 当需要处理混合寻址(IPv6+IPv4)时,使用第三种格式。在这种情况下,前12个字节用于IPv6寻址,其余4个字节用于IPv4寻址。例如
F334::40CB:152.16.24.142
- ::FFFF:d.d.d.d- 这种类型的寻址称为 IPv4映射寻址 。它用于帮助部署IPv6寻址。它允许使用相同的结构和套接字通过IPv6和IPv4连接的网络进行通信。前80位由“:”表示的0填充。接下来的32位都是“1”,剩下的32位代表IPv4地址。例如
::FFFF:152.16.24.123
方法:
- getByAddress(字符串主机,字节[]地址,int scope_id): 这用于通过将IPv6作用域id设置为给定值来创建Inet6Address对象。返回的对象与InetAddress创建的对象类似。getByAddress(字符串,字节[]),包含有关作用域id的其他信息。
Syntax :public static Inet6Address getByAddress(String host, byte[] addr, int scope_id) throws UnknownHostException Parameters : host : host addr : raw ip address in network order scope_id : scope id of the address Throws : UnknownHostException : if IP address is of illegal length
- getByAddress(字符串主机,字节[]地址,网络接口nif): 可以使用重载方法getByAddress()指定要与地址一起使用的网络接口。在这种情况下,与网络接口对应的作用域id被用作作用域id。
Syntax :public static Inet6Address getByAddress(String host, byte[] addr, NetworkInterface nif) throws UnknownHostException Parameters : host : host addr : raw ip address in network order nif : network interface to be associated with this address Throws : UnknownHostException : if IP address is of illegal length
- getScopeId(): 返回与此地址关联的作用域id,如果未设置,则返回0。
Syntax : public int getScopeId()
- getScopedInterface(): 返回与此地址关联的网络接口,如果未设置,则返回null。
Syntax : public NetworkInterface getScopedInterface()
- getAddress(): 以数组形式返回此InetAddress对象的原始IP地址。数组中字节的显示顺序与IP地址中的相同,即getAddress[0]将包含最高顺序的字节。
Syntax : public byte[] getAddress()
- getHostAddress(): 以文本形式返回IP地址。
Syntax :public String getHostAddress()
- isAnyLocalAddress(): 如果此地址代表本地地址,则返回true。
Syntax :public boolean isAnyLocalAddress()
- IsLink LocalAddress(): 如果此地址是链接本地地址,则返回true。
Syntax :public boolean isLinkLocalAddress()
- isLoopbackAddress(): 如果此地址是环回地址,则返回true。
Syntax :public boolean isLoopbackAddress()
- isMCGlobal(): 如果此多播地址具有全局作用域,则返回true。
Syntax :public boolean isMCGloabal()
- isMCLinkLocal(): 如果此多播地址具有链接作用域,则返回true。
Syntax :public boolean isMCLinkLocal()
- isMCNodeLocal(): 如果此多播地址具有节点作用域,则返回true。
Syntax :public boolean isMCNodeLocal()
- isMCOrgLocal(): 如果此多播地址具有组织作用域,则返回true。
Syntax :public boolean isMCOrgLoacal()
- isMCSiteLocal(): 如果此多播地址具有站点作用域,则返回true。
Syntax :public boolean isMCSiteLocal()
- isMulticastAddress(): 如果此地址是IP多播地址,则返回true。多播地址的前4位是1110。
Syntax :public boolean isMulticastAddress()
- isSiteLocalAddress(): 如果此地址是站点本地地址,则返回true。
Syntax :public boolean isSiteLocalAddress()()
- hashCode(): 返回与此地址对象关联的哈希代码。
Syntax : public int hashCode()
- 等于 :如果此ip地址与指定对象的ip地址相同,则返回true。在比较时,不要考虑主机名,只考虑相关的IP地址。
Syntax : public boolean equals(Object obj) Parameters : obj : object to compare with
Java实现:
//Java program to illustrate various //Inet6Address class methods import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; public class inet6add { public static void main(String[] args) throws UnknownHostException { String host = "localhost" ; byte add[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 }; //getByAddress() method Inet6Address ip1 = Inet6Address.getByAddress(host, add, 5 ); Inet6Address ip2 = Inet6Address.getByAddress( null , add, 6 ); // Following methods checks the property of the thus created object. // getscopeId() method System.out.println( "Scope Id : " + ip1.getScopeId()); // getScopedInterface() method System.out.println( "Scoped Interface : " + ip1.getScopedInterface()); // 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)); } } |
输出:
Scope Id : 5 Scoped Interface : null Address : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] Host Address : 0:0:0:0:0:0:0:1%5 isAnyLocalAddress : false isLinkLocalAddress : false isLoopbackAddress : true isMCGlobal : false isMCLinkLocal : false isMCNodeLocal : false isMCOrgLocal : false isMCSiteLocal : false isMulticastAddress : false isSiteLocalAddress : false hashCode : 1 ip1==ip2 : true
本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END