java.net.InetAddress example
27 June 2008Each 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.
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."); }
Related Posts:
- No related posts
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: java.net.InetAddress example