InetAddress Example program in Java

By: Grenfel Printer Friendly Format    


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( )
throws UnknownHostException
static InetAddress getByName(String hostName)
throws UnknownHostException
static InetAddress[ ] getAllByName(String hostName)
throws UnknownHostException

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.
import java.net.*;
class InetAddressTest
{
public static void main(String args[]) throws
UnknownHostException {
InetAddress Address = InetAddress.getLocalHost();
System.out.println(Address);
Address = InetAddress.getByName("starwave.com");
System.out.println(Address);
InetAddress SW[] = InetAddress.getAllByName("www.nba.com");
for (int i=0; i<SW.length; i++)
System.out.println(SW[i]);
}
}

Here is the output produced by this program. (Of course, the output you see will be slightly different.)

default/206.148.209.138
starwave.com/204.202.129.90
www.nba.com/204.202.130.223

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".



Ask a Question



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Ask a Question

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. more info needs to be given ............
View Tutorial          By: sufficient for new commer at 2008-05-09 00:41:58

2. excellent for a beginer in inet address ...
View Tutorial          By: Tony at 2009-08-13 22:39:48

3. Can you give me an example to create InetAddress f
View Tutorial          By: Vaibhav at 2009-12-15 23:37:49

4. Would be great if you also showed how to manipulat
View Tutorial          By: Sparx at 2010-01-05 13:51:56

5. good but very simple program.........
View Tutorial          By: vipul at 2010-03-25 00:33:54

6. it shows error
java.lang.NoClassDefFoundEr

View Tutorial          By: vamsi at 2010-05-14 23:09:03

7. excellent notes
View Tutorial          By: jawahar at 2010-08-03 00:23:52

8. I am getting

jayana-desktop/127.0.

View Tutorial          By: GSA at 2010-11-23 02:36:26

9. @Vaibhavn if you use an IP instead of a host name
View Tutorial          By: RDMA at 2011-01-04 14:30:15

10. Excellent program...
View Tutorial          By: chitresh at 2011-02-03 00:11:45

11. its really good work. and i need more documentatio
View Tutorial          By: Rajesh at 2011-03-01 04:45:52

12. Does anybody know how to force using IPv6 by my la
View Tutorial          By: Marcin at 2011-05-17 05:24:51

13. HI all i would like to send the result of " I
View Tutorial          By: FLORIAN at 2011-09-14 09:04:42

14. how can get ip url...?
View Tutorial          By: arjuna at 2011-11-23 03:31:46

15. Useful!
View Tutorial          By: Troll at 2011-12-12 00:53:00

16. nice 1... can u plz provide some code to work with
View Tutorial          By: ashu at 2012-04-02 05:14:14

17. Allodaydere
View Tutorial          By: Allodaydere at 2013-05-16 09:32:52

18. How to display the ARP Mapping Table of a given ho
View Tutorial          By: Hala at 2013-11-15 22:42:36

19. HI, nice example you may refer following one also<
View Tutorial          By: Sreekanth Gde at 2014-11-25 16:35:24

20. It is showing error in SW??
kindly tell, ho

View Tutorial          By: Jeet singh at 2016-03-14 20:09:10