Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 885
TiansHUo Avatar asked Feb 22 '10 05:02

TiansHUo


People also ask

How do I convert a string into an Internet address?

in_addr_t inet_addr(const char *strptr) This function call converts the specified string in the Internet standard dot notation to an integer value suitable for use as an Internet address.

Can InetAddress be used with I need 6?

An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses.


1 Answers

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.

like image 56
John Gardiner Myers Avatar answered Sep 21 '22 23:09

John Gardiner Myers