Getting the IP Address of a Hostname

try { InetAddress addr = InetAddress.getByName("javaalmanac.com"); byte[] ipAddr = addr.getAddress(); // Convert to dot representation String ipAddrStr = ""; for (int i=0; i<ipAddr.length; i++) { if (i > 0) { ipAddrStr += "."; } ipAddrStr += ipAddr[i]&0xFF; } } catch (UnknownHostException e) { }

Comments

18 Jan 2010 - 1:13pm by john.. (not verified)

good code.. but what
isthe post of the code and prefix

7 Feb 2010 - 10:01pm by Manickaraj (not verified)

Very very use full for me, so many thanks

9 Mar 2010 - 12:22pm by Anonymous (not verified)

Why go through the hassle of formatting the byte array, when "getHostAddress()" gives you a formatted string?

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.