GVKun编程网logo

InetAddress.getHostAddress()是否符合ipv6?(getaddrinfo ipv6)

18

本文将带您了解关于InetAddress.getHostAddress的新内容,同时我们还将为您解释是否符合ipv6?的相关知识,另外,我们还将为您提供关于(OK)Lineage-14.1(Andro

本文将带您了解关于InetAddress.getHostAddress的新内容,同时我们还将为您解释是否符合ipv6?的相关知识,另外,我们还将为您提供关于(OK) Lineage-14.1(Android 7) - enable eth0 to get ipv6 address、7.1(java学习笔记)InetAddress&InetScoketAddress、android – InetAddress.java调用的“getaddrinfo”的实现在哪里、asp.net – Page.Request.UserHostAddress的格式无效的实用信息。

本文目录一览:

InetAddress.getHostAddress()是否符合ipv6?(getaddrinfo ipv6)

InetAddress.getHostAddress()是否符合ipv6?(getaddrinfo ipv6)


InetAddress.getHostAddress()的IPv6兼容的JDK
1.6?

我具体在做

InetAddress.getLocalHost().getHostAddress()

是否符合ipv6?它对ipv4和v6地址都有效吗?

答案1

小编典典

我查看了InetAddress类的代码,它确实在做正确的事情。

  if (isIPv6Supported()) {       o = InetAddress.loadImpl("Inet6AddressImpl");   }   else {       o = InetAddress.loadImpl("Inet4AddressImpl"); }       return (InetAddressImpl)o;   }

(OK) Lineage-14.1(Android 7) - enable eth0 to get ipv6 address

(OK) Lineage-14.1(Android 7) - enable eth0 to get ipv6 address


if  "ip -6 addr add"  -->  "Permission denied"

then

echo 0 > /proc/sys/net/ipv6/conf/eth0/disable_ipv6





7.1(java学习笔记)InetAddress&InetScoketAddress

7.1(java学习笔记)InetAddress&InetScoketAddress

一、InetAddress

   这个类主要表示IP地址.InetAddress封装了IP地址和域名。域名可以看做IP地址的一个别称,起这个别称的目的是为了便于记忆。

   例如www.baidu.com 就是119.75.217.109的别称,在浏览器地址栏输入119.75.217.109同样可以访问百度首页。明显这个别称更好记忆。

   InetAddress封装了IP地址和域名,总之里面的Address就是IP地址,Name就是这个地址的别称。

  

  1.1构造方法,改类的构造方法无法直接调用,需通过返回值为InetAddress的方法来获取对象。

    static InetAddress getByName(String host)//根据主机名(可以看做本地电脑的域名)返回InetAddress对象。

    host可为IP地址、域名的字符串表示。

    static InetAddress getByAddress(byte[] addr)//初始化参数为ip地址的字节数组表示,

    例如 new byte[]{(byte)192,(byte)168,(byte)0,(byte)81}

    static InetAddress getByAddress(String host,Byte[] addr)//返回host,addr设置的对应对象

    static InetAddress getLocalHost()//返回代表本机IP、域名的InetAddress对象。

  

  2.2主要方法

    String getHostName()//获取该对象的主机名

    String getHostAddress()//获取该对象的IP地址

 

  2.3 例子

    

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestNet {
    public static void main(String[] args) throws UnknownHostException {
        //InetAddress 封装了IP和DNS(域名)
        InetAddress localhost = InetAddress.getLocalHost();//返回代表本地InetAddress对象
        System.out.println(localhost.getHostName());//获取该该对象的IP域名     Ni
        System.out.println(localhost.getHostAddress());//获取该对象的IP地址  192.168.17.1
        InetAddress a = InetAddress.getByName("localhost");//返回该域名所代表的InetAddres对象
        System.out.println(a.getHostName());//获取该对象代表的域名               localhost
        System.out.println(a.getHostAddress());//获取该对象代表的IP地址 127.0.0.1
        InetAddress b = InetAddress.getByName("192.168.17.1");//返回该IP地址所代表的InetAddress对象
        System.out.println(b.getHostName());//返回该对象所代表域名    Ni
        System.out.println(b.getHostAddress());//返回该对象的IP地址 192.168.17.1
    }
}
Ni
192.168.17.1
localhost
127.0.0.1
Ni
192.168.17.1

 上述使用的都是本机地址,所以都可以将IP地址转换为域名。但某些其他外部网络地址会出现无法通过IP解析成域名的情况。

 

API中有这样一句话:

InetAddress类提供了将主机名解析为IP地址的方法,反之亦然。

将IP地址解析为域名时,如果存在安全管理器,则此方法首先使用主机名和-1作为参数调用其checkConnect方法,

以查看是否允许调用代码知道此IP地址的主机名,即是否连接到主机。如果不允许操作,它将返回IP地址的文本表示。

如果允许则返回域名。

 

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class TestNet {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress b = InetAddress.getByName("www.baidu.com");
        System.out.println(b.getHostName());
        System.out.println(b.getHostAddress());
    }
}
运行结果:
www.baidu.com
119.75.217.109

如果将static InetAddress getByName(String host)中的host设置为119.75.217.109,

会导致无法解析的情况发生,这时返回的域名就是这个IP地址本身的文本表示。

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class TestNet {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress b = InetAddress.getByName("119.75.217.109");
        System.out.println(b.getHostName());
        System.out.println(b.getHostAddress());
    }
}
运行结果;
119.75.217.109
119.75.217.109

 简而言之,如果能将IP地址解析成域名getHostName就返回域名,如果不能则返回IP地址本身。

 

二、InetSocketAddress

  InetSocketAddress在InetAddress的基础上封装了端口。

  

  2.1构造方法

  InetSocketAddress(InetAddress addr, int port)

  InetSocketAddress(String hostname, int port)

  //构造方法,hostname可为ip地址也可为主机名 port端口号

  传递参数为String 类型的ostname实际上内部是将String hostname转换为了InetAddress addr。

  

  其中的

  

  

  2.2主要方法

  int getPort()//获取端口号

  String getHostName()//获取主机名
  InetAddress getAddress()//返回一个InetAddress对象

 

  2.3例子

   

import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class TestNet {
    public static void main(String[] args) throws UnknownHostException {
        //最好不要分配1024以下的端口
        InetSocketAddress i = new InetSocketAddress("localhost",2048);//初始化,域名|主机名|地址
        //InetSocketAddress e = new InetSocketAddress("127.0.0.1",2049);
        System.out.println(i.getAddress().getHostName());
        System.out.println(i.getPort());//端口号
        InetAddress ia = i.getAddress();//返回一个InetAddress对象
        System.out.println(ia.getHostName());//后续就可以调用InetAddress中的方法对其操作 (主机名)
        System.out.println(ia.getHostAddress());//主机IP地址
    }
}
运行结果:
localhost
2048
localhost
127.0.0.1

android – InetAddress.java调用的“getaddrinfo”的实现在哪里

android – InetAddress.java调用的“getaddrinfo”的实现在哪里

我最近一直在研究为什么Android操作系统中的浏览器
不支持通过IPv6 URL访问IPv6网站
我认为这是由于本机方法getaddrinfo由android / dalvik / libcore / luni / src / main / java / java / net /调用
InetAddress.java,然后由浏览器应用程序调用
尝试将IPv6 URL转换为时,会抛出UnkNownHostException
它的地址.

从第507行开始,InetAddress.java的代码是:

try {   
    InetAddress[] addresses = bytesToInetAddresses(getaddrinfo(host), host);   
    addressCache.put(host, addresses);   
    return addresses;

并且本机方法在第516行声明为:

private static native byte[][] getaddrinfo(String name) 
        throws UnkNownHostException; 

但我还没有找到任何暗示这个实施的地方
本机方法getaddrinfo虽然我发现有两个文件
名为getaddrinfo.c.这里遵循JNI或NDK的规则吗?如果
所以,System.loadLibary(“NameOfTheLibrary”)的声明应该是
发现但我没有找到它.
任何人都可以给我一个暗示找到这个的实现
本地方法getaddrinfo?

可以在http://ooowjc.wikispaces.com/Attachments找到InetAddress.java的完整源文件

解决方法:

在Android源代码树中,libcore / luni / src / main / native / java_net_InetAddress.cpp.

我在android-git存储库中包含了一个指向该文件的链接,但是site目前似乎已被清除.

asp.net – Page.Request.UserHostAddress的格式无效

asp.net – Page.Request.UserHostAddress的格式无效

有时我的ASP.NET应用程序在查询Page.Request.UserHostAddress时返回值“:: 1”.

我看不到任何关于何时返回正确的IP地址与返回格式错误的字符串的模式.它将工作半天,然后随机停止工作一个小时.有什么想法吗?

编辑:

好的,它是IPv6的环回地址. :)这导致了进一步的问题……何时/为什么在某些情况下使用IPv6而不是其他情况?

解决方法

:: 1是IPv6环回地址.相当于IPv4的127.0.0.1.

我们今天的关于InetAddress.getHostAddress是否符合ipv6?的分享就到这里,谢谢您的阅读,如果想了解更多关于(OK) Lineage-14.1(Android 7) - enable eth0 to get ipv6 address、7.1(java学习笔记)InetAddress&InetScoketAddress、android – InetAddress.java调用的“getaddrinfo”的实现在哪里、asp.net – Page.Request.UserHostAddress的格式无效的相关信息,可以在本站进行搜索。

本文标签: