26

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

Is there a ready method for that, or do I need to split the String and create the InetAddress from its bytes?

Update The 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.

4

4 Answers 4

30

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.

java source code

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

}
Sign up to request clarification or add additional context in comments.

12 Comments

Is it guaranteed there will be no DNS going on (also no reverse DNS or whatever) ?
@BartFriederichs As per java document there is no DNS verification if correct ip format is provided. Reference - docs.oracle.com/javase/7/docs/api/java/net/…
Source code says the same: 'if host is an IP address, we won't do further lookup'
If the supplied string is in dotted quad format, it will not do a dns lookup. If you need to be sure, validate the string first, e.g. . stackoverflow.com/questions/5667371/validate-ip-address
In my case, InetAddress address= InetAddress.getByName("www.sun.com"); runs correctly, but when I give a wrong host address as the argument such as "www.sunqqq.com" it gave exceptions. >>> java.net.UnknownHostException: www.sunqqq.com at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source) at java.net.InetAddress.getAddressesFromNameService(Unknown Source) at java.net.InetAddress.getAllByName0(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source)....(more)
|
7

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

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

(emphasis added)

Comments

3

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

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
        }

Comments

1

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

InetAddress localhost = InetAddress.getByName("127.0.0.1")

As it is described on the java docs:

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.

3 Comments

In my case, InetAddress address= InetAddress.getByName("www.sun.com"); runs correctly, but when I give a wrong host address as the argument such as "www.sunqqq.com" it gave exceptions. >>> java.net.UnknownHostException: www.sunqqq.com at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source) at java.net.InetAddress.getAddressesFromNameService(Unknown Source) at java.net.InetAddress.getAllByName0(Unknown Source) at java.net.InetAddress.getAllByName(Unknown Source)....(more) –
So does it means that getByName() method do the DNS checkups??? As it seems the validity of the host address also have checked???
Yes. if you give a host name to getByName, it perform DNS lookup. but if an IP address is provided it only checks for the validity of the IP (string format).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.