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

20,982

Solution 1

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.

Solution 2

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.

Solution 3

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!

Solution 4

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.

Share:
20,982
TiansHUo
Author by

TiansHUo

#define _ F-->00 || F-OO--; long F=00,OO=00; main(){F_OO();printf("%1.3f\n", 4.*-F/OO/OO);}F_OO() { _-_-_-_ _-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_-_-_-_ _-_-_-_-_-_-_-_-_ _-_-_-_ }

Updated on December 18, 2020

Comments

  • TiansHUo
    TiansHUo over 3 years

    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?

  • TiansHUo
    TiansHUo over 14 years
    Thanks, I looked up the api, and missed that line.
  • Sebastian Hoffmann
    Sebastian Hoffmann over 12 years
    Whats when host is a pattern like 192.168.0.*? Will that work too? Regarding to Inet4Address doc there seems support for this.
  • Matthew Flaschen
    Matthew Flaschen over 12 years
    @Paranaix, no, it will throw a IllegalArgumentException with the message "invalid host wildcard specification"
  • Andre Holzner
    Andre Holzner about 12 years
    in particular, the documentation says: If a literal IP address is supplied, only the validity of the address format is checked. which I read as: 'if you specify a (dotted quad notation) IP address, no DNS lookup is performed'.
  • Matthew Flaschen
    Matthew Flaschen about 12 years
    This is part of Guava.
  • Raedwald
    Raedwald about 11 years
    The OP said "I am not trying to resolve hostnames"; if the input to getByName() is not a valid numeric IP address, but is a valid resolvable DNS name, the name will be resolved. That does not seem to be what the OP wants.
  • Matthew Flaschen
    Matthew Flaschen about 11 years
    @Raedwald, he also said "the strings are ipv4 addresses". If the input is known to be just IP addresses, this should work fine.
  • Craig Trader
    Craig Trader over 9 years
    As of Java 7, this will throw an UnknownHostException; haven't checked earlier Javas, but I would expect an exception there as well.
  • silmeth
    silmeth over 7 years
    It should be com.google.common.net.InetAddresses.forString (with InetAddresses with uppercase A). SO won’t allow me to correct that in your answer, as the edit has to be at least 6 characters. ;-) Anyway, thanks, was looking for it, and yet again Guava saves the day.
  • Marcono1234
    Marcono1234 almost 3 years
    @CraigTrader, but if you look at the stack trace you will see that this is not actually a parsing exception, but Java tries to look this up as host name, which is somewhat worrying.
  • Craig Trader
    Craig Trader almost 3 years
    Because 999.999.999.999 is NOT a valid IPv4 address, Java asks the system's name server to resolve it as a hostname. Since there is no valid DNS entry for that IP address, you'll get an UnknownHostException. If you try this with a valid IPv4 address, you'll get a valid InetAddress object.