View RSS Feed

My Java Tips

java.net.InetAddress example

Rate this Entry
by , 11-03-2011 at 12:15 PM (2128 Views)
Each computer on internet is called a node or a host. Each host has unique address called Internet Protocol (IP) address. IP addresses are not easy for the humans to remember therefore, Domain Name System (DNS) was introduced. DNS assigns a human understandable name to IP addresses.




InetAddress class represents IP addresses which can be useful in networking environment. Let see what we can o with this one:

Following code finds and prints the IP address of a given web host. Please note that some hosts have more than one IP addresses therefore, we will get all the IP addresses in an array and then will print those.

Java Code:
try {
InetAddress[] addresses =
InetAddress.getAllByName("www.cnn.com");
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i]);
}
}
catch (UnknownHostException ex) {
System.out.println("Could not find the host.");
}

Submit "java.net.InetAddress example" to Facebook Submit "java.net.InetAddress example" to Digg Submit "java.net.InetAddress example" to del.icio.us Submit "java.net.InetAddress example" to StumbleUpon Submit "java.net.InetAddress example" to Google

Tags: None Add / Edit Tags
Categories
Java SE , Network Programming

Comments