TiansHUo
TiansHUo

Reputation: 8629

Is there an easy way to convert String to Inetaddress in Java?

I am trying to convert strings into Inetaddress. I am not trying to resolve hostnames: the strings are ipv4 addresses. Does InetAddress.getByName(String host) work? Or do I have to manually parse it?

Upvotes: 31

Views: 23084

Answers (7)

Matthew Flaschen
Matthew Flaschen

Reputation: 285017

Yes, that will work. The API is very clear on this ("The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address."), and of course you could easily check yourself.

EDIT: If you want to ensure DNS is not queried, I recommend John Gardiner Myers's answer. You should not need to query DNS if the input is an IP address.

Upvotes: 15

ZhekaKozlov
ZhekaKozlov

Reputation: 39614

Since Java 22:

jshell> InetAddress addr = InetAddress.ofLiteral("127.0.0.1")
addr ==> /127.0.0.1

jshell> InetAddress addr = InetAddress.ofLiteral("::0")
addr ==> /0:0:0:0:0:0:0:0

jshell> InetAddress addr = InetAddress.ofLiteral("999.999.999.999")
|  Exception java.lang.IllegalArgumentException: Invalid IP address literal: 999.999.999.999
|        at IPAddressUtil.invalidIpAddressLiteral (IPAddressUtil.java:169)
|        at Inet6Address.ofLiteral (Inet6Address.java:534)
|        at InetAddress.ofLiteral (InetAddress.java:1735)
|        at (#7:1)

Upvotes: 0

gerardnico
gerardnico

Reputation: 1073

With the DNSJava library, you can also parse it.

InetAddress address = Address.getByAddress(ipString)

No external call, just using the Address.toByteArray method.

Upvotes: 0

Sean F
Sean F

Reputation: 4615

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 s = "1.2.3.4";
     try {
            IPAddressString str = new IPAddressString(s);
            IPAddress addr = str.toAddress();
            InetAddress inetAddress = addr.toInetAddress(); //IPv4 or IPv6
            if(addr.isIPv4() || addr.isIPv4Convertible()) {//IPv4 specific
                IPv4Address ipv4Addr = addr.toIPv4();
                Inet4Address inetAddr = ipv4Addr.toInetAddress();
                //use address
            }
      } catch(AddressStringException e) {
            //e.getMessage has validation error
      }

Upvotes: 0

John Gardiner Myers
John Gardiner Myers

Reputation: 294

com.google.common.net.InetAddresses.forString(String ipString) is better for this as it will not do a DNS lookup regardless of what string is passed to it.

Upvotes: 27

wu-lee
wu-lee

Reputation: 769

Beware: it seems that parsing an invalid address such as InetAddress.getByName("999.999.999.999") will not result in an exception as one might expect from the documentation's phrase:

the validity of the address format is checked

Empirically, I find myself getting an InetAddress instance with the local machine's raw IP address and the invalid IP address as the host name. Certainly this was not what I expected!

Upvotes: 4

Raedwald
Raedwald

Reputation: 48702

You could try using a regular expression to filter-out non-numeric IP addresses before passing the String to getByName(). Then getByName() will not try name resolution.

Upvotes: 1

Related Questions