TO GET LOCAL HOST ADDRESS - Computer Programming


C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Sunday, October 30, 2011



            In order to tell one machine from another and to make sure that you are connected with the machine you want, there must be some way of uniquely identifying machines on a network. Early networks were satisfied to provide unique names for machines within the local network. However, Java works within the Internet, which requires a way to uniquely identify a machine from all the others in the world. This is accomplished with the IP (Internet Protocol) address that can exist in two forms:
1. The familiar DNS (Domain Name Service) form.
 My domain name is, so suppose I have a computer called   Opus in my domain. Its domain name would be This is exactly the kind of name that you use when you send email to people, and is often incorporated into a World-Wide-Web address. 
2. Alternatively, you can use the “dotted quad” form, which is four numbers separated by dots, such as 
In both cases, the IP address is represented internally as a 32-bit number1 (so each of the quad numbers cannot exceed 255), and you can get a special Java object to represent this number from either of the forms above by using the static InetAddress.getByName( ) method that’s in The result is an object of type InetAddress that you can use to build a “socket” .
      This can sometimes be a handy way to distribute information to someone else or to test out a Web site configuration before posting it to a “real” server. 
Servers and clients the whole point of a network is to allow two machines to connect and talk to each other.
The generic way to produce this address in Java is: 
 InetAddress addr = InetAddress.getByName(null);
   If you hand getByName( ) a null, it defaults to using the localhost.
   The InetAddress is what you use to refer to the particular machine, and you must produce this before you can go any further. The only way you can create an InetAddress is through one of that class’s static member methods getByName( ) , getAllByName( ), or getLocalHost( ). 
You can also produce the local loopback address by handing it the string localhost: InetAddress.getByName("localhost");
or by using its dotted quad form to name the reserved IP number for the loopback: InetAddress.getByName("");
All three forms produce the same result. 
Port: a unique place within the machine
 1.  static InetAddress getLocalHost()
      throws UnknownHostException
The getLocalHost() method simply returns the InetAddress object that represents
 the local host.

2.  static InetAddress getByName(String hostname)
                          throws UnknownHostException
The getByName() method returns an InetAddress for a host name passed to it.
3.  static InetAddress[] getAllByName(String hostname)
                                 throws UnknownHostException
If these methods are unable to resolve the host
name,they thjrow an UnknownHostException.


D:\J2SDK\BIN>java localaddress




No comments:

Post a Comment