Is there an easy way to convert String to Inetaddress in Java?
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.
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.
Tested and working: int ip = … ; String ipStr = String.format(“%d.%d.%d.%d”, (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
There are a few difference between the two: getCanonicalHostName() will attempt to resolve the FQDN. Therefore, you would get foo.mycompany.com whereas getHostName() might just return foo. getCanonicalHostName() will always do a reverse DNS lookup, whereas getHostName() would return the stored hostname if you supplied one in the InetAddress constructor. I suspect you will be wanting … Read more
Try this: InetAddress address = InetAddress.getByName(new URL(urlString).getHost()); To get the raw IP: String ip = address.getHostAddress();
Simply call InetAddress.getByName(String host) passing in your textual IP address. From the javadoc: The host name can either be a machine name, such as “java.sun.com”, or a textual representation of its IP address. InetAddress javadoc
The “isReachable” method has not been worthy of using for me in many cases. You can scroll to the bottom to see my alternative for simply testing if you’re online and capable of resolving external hosts (i.e. google.com) … Which generally seems to work on *NIX machines. The issue There is alot of chatter about … Read more