Java 无需 DNS 查找即可将字符串转换为 InetAddress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20519491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:30:37  来源:igfitidea点击:

Convert a String to InetAddress without DNS lookup

javaip-address

提问by Bart Friederichs

I have a local IP address in dotted decimal notation in a String. I want to convert it to an InetAddressto feed it to Socket, but I need to do it without doing a DNS lookup (because this might cause lengthy timeouts).

我在String. 我想将其转换为 anInetAddress以将其提供给Socket,但我需要在不进行 DNS 查找的情况下执行此操作(因为这可能会导致长时间超时)。

Is there a ready method for that, or do I need to split the Stringand create the InetAddressfrom its bytes?

是否有现成的方法,或者我是否需要从其字节拆分String和创建InetAddress

UpdateThe factory methods InetAddress.getByName()and InetAddress.getByAddress()don't seem to be a good fit, as they both also accept hostnames such as java.sun.com. There is no saying if they will try to contact a DNS server in their implementation.

更新工厂方法InetAddress.getByName()InetAddress.getByAddress()似乎不太合适,因为它们都接受主机名,例如java.sun.com. 没有人说他们是否会在他们的实施中尝试联系 DNS 服务器。

采纳答案by Prabhakaran Ramaswamy

Do like this

这样做

InetAddress inetAddress = InetAddress.getByName("192.168.0.105");

If a literal IP address is supplied, only the validity of the address format is checked.

如果提供了文字 IP 地址,则仅检查地址格式的有效性。

java source code

java源代码

// if host is an IP address, we won't do further lookup    
if (Character.digit(host.charAt(0), 16) != -1 || (host.charAt(0) == ':')) {

}
// if host is an IP address, we won't do further lookup    
if (Character.digit(host.charAt(0), 16) != -1 || (host.charAt(0) == ':')) {

}

回答by Ali

You can do this by using the getByName method. for example:

您可以使用 getByName 方法执行此操作。例如:

InetAddress localhost = InetAddress.getByName("127.0.0.1")

As it is described on the java docs:

正如 java 文档中所描述的:

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

主机名可以是机器名称,例如“java.sun.com”,也可以是其 IP 地址的文本表示。如果提供了文字 IP 地址,则仅检查地址格式的有效性。

回答by Matt Ball

You can use Guava's InetAddresses#forString()which is specifically documented for your use case:

您可以使用专门针对您的用例记录的GuavaInetAddresses#forString()

Returns the InetAddresshaving the given string representation.
This deliberately avoids all nameservice lookups (e.g. no DNS).

返回InetAddress具有给定字符串表示的 。
这特意避免了所有名称服务查找(例如,没有 DNS)。

(emphasis added)

(强调)

回答by Sean F

The open-source IPAddress Java librarywill validate all standard representations of IPv6 and IPv4 and will do so without DNS lookup. Disclaimer: I am the project manager of that library.

开源 IPAddress Java 库将验证 IPv6 和 IPv4 的所有标准表示,并且无需 DNS 查找。免责声明:我是那个图书馆的项目经理。

The following code will do what you are requesting:

以下代码将执行您的要求:

        String str = "fe80:0:0:0:f06c:31b8:cd17:5a44";
        try {
            IPAddressString str = new IPAddressString(str);
            IPAddress addr = str.toAddress();//throws if invalid, without a DNS lookup
            InetAddress inetAddr = addr.toInetAddress();//convert valid address
            //use address
        } catch(AddressStringException e) {
            //e.getMessage has validation error
        }