InetAddress Example program in Java
The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class. This is done in lieu of overloading a constructor with various parameter lists when having unique method names makes the results much clearer. In the case of InetAddress, the three methods getLocalHost(), getByName(), and getAllByName() can be used to create instances of InetAddress. These methods are shown here:
static InetAddress getLocalHost( )
static InetAddress getByName(String hostName)
static InetAddress[ ] getAllByName(String hostName)
The getLocalHost( ) method simply returns the InetAddress object that represents the local host. The getByName( ) method returns an InetAddress for a host name passed to it. If these methods are unable to resolve the host name, they throw an UnknownHostException.
On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can't resolve the name to at least one address.
The following example prints the addresses and names of the local machine and two well-known Internet web sites:
// Demonstrate InetAddress.
public static void main(String args) throws
InetAddress Address = InetAddress.getLocalHost();
Address = InetAddress.getByName("starwave.com");
InetAddress SW = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
Here is the output produced by this program. (Of course, the output you see will be slightly different.)
This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".
1. It is showing error in SW??
kindly tell, how can I correct this
View Tutorial By: Jeet singh at 2016-03-14 20:09:10
2. HI, nice example you may refer following one also
View Tutorial By: Sreekanth Gde at 2014-11-25 16:35:24
3. How to display the ARP Mapping Table of a given host ?
View Tutorial By: Hala at 2013-11-15 22:42:36
View Tutorial By: Allodaydere at 2013-05-16 09:32:52
5. nice 1... can u plz provide some code to work with ICMP ping
View Tutorial By: ashu at 2012-04-02 05:14:14
View Tutorial By: Troll at 2011-12-12 00:53:00
7. how can get ip url...?
View Tutorial By: arjuna at 2011-11-23 03:31:46
8. HI all i would like to send the result of " InetAddress Address = InetAddress.getLocalHost(); &
View Tutorial By: FLORIAN at 2011-09-14 09:04:42
9. Does anybody know how to force using IPv6 by my laptop??
I connect my computer to another one
View Tutorial By: Marcin at 2011-05-17 05:24:51
10. its really good work. and i need more documentation on it. but tats cool post lol
View Tutorial By: Rajesh at 2011-03-01 04:45:52
11. Excellent program...
View Tutorial By: chitresh at 2011-02-03 00:11:45
12. @Vaibhavn if you use an IP instead of a host name it works too!
View Tutorial By: RDMA at 2011-01-04 14:30:15
13. I am getting
Exception in thread "main" ja
View Tutorial By: GSA at 2010-11-23 02:36:26
14. excellent notes
View Tutorial By: jawahar at 2010-08-03 00:23:52
15. it shows error
View Tutorial By: vamsi at 2010-05-14 23:09:03
16. good but very simple program.........
View Tutorial By: vipul at 2010-03-25 00:33:54
17. Would be great if you also showed how to manipulate the output too.
View Tutorial By: Sparx at 2010-01-05 13:51:56
18. Can you give me an example to create InetAddress from IP address like 188.8.131.52
View Tutorial By: Vaibhav at 2009-12-15 23:37:49
19. excellent for a beginer in inet address ...
View Tutorial By: Tony at 2009-08-13 22:39:48
20. more info needs to be given ............
View Tutorial By: sufficient for new commer at 2008-05-09 00:41:58
Comment on this tutorial
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java