We start a Java Internet application by importing java.net.*
, that
contains the core networking routines. It contains the InetAddress
class, that
allows us a simple method of converting between numeric and DNS form addresses.
Here is an example of using the InetAddress
class. The source code is below
and in NetApp1.java
.
1 2 3 // An application to find out the current address and DNS name 4 // of the local computer - Derek Molloy. 5 6 import java.net.*; 7 8 public class NetApp1 9 { 10 public static void main(String args[]) 11 { 12 try // try to do the following 13 { 14 // get local address 15 InetAddress localaddr = InetAddress.getLocalHost(); 16 // find the dns name 17 String hostName = localaddr.getHostName(); 18 System.out.println ("Local IP Address : " + localaddr); 19 System.out.println ("Local hostname : " + hostName); 20 } 21 catch (UnknownHostException e) // catch the unknown host exception 22 { 23 System.err.println ("Can't detect localhost : " + e); 24 } 25 } 26 } 27 28
This simple application shows us how Java treats networked applications. Again the class
structure comes into play where methods associated with the Internet addresses are a part of the
InetAddress
class. This application is compiled by typing
javac NetApp1.java and run by typing java NetApp1. The output
looks like Figure 10.1, “InetAddress
Class Example”.
In this case the local IP address is listed as portalm/136.206.37.8
- the Microsoft
Networks name of my PC, and the fixed IP address as I was not connected
to the Internet with, when I ran the application. The local hostname is extracted using the
getHostName()
method and returns the String
object
"portalm"
.
We can modify the code that we just wrote to create a simple "Who Is?" application. If can write
this simply by the following code (as below and in
WhoIs.java
).
1 2 3 // An application to find out the address of any named 4 // computer - Derek Molloy. 5 6 import java.net.*; 7 8 public class WhoIs 9 { 10 public static void main(String args[]) 11 { 12 if(args.length != 1) 13 { 14 System.err.println("Usage: WhoIs [Name]"); 15 System.exit(0); 16 } 17 try{ 18 InetAddress a = InetAddress.getByName(args[0]); 19 System.out.println(a.toString()); 20 } 21 catch(UnknownHostException e) 22 { 23 System.out.println("Unknown Host Exception!"); 24 } 25 } 26 } 27 28
When this application is run it expects the name of a machine, either specified by the full Internet name, or the name of a machine on the same network segment. If you forget to provide the parameters, it prompts you for the correct parameters (See Figure 10.2, “WhoIs Example”)
In Figure 10.2, “WhoIs Example” the first time I ran java WhoIs I did not pass the
hostname parameter so I was prompted to do so. The second time I ran java WhoIs www.cnn.com
and this worked well, returning the IP address 64.236.16.20
. This could change each time
you run the command as there would be several servers for www.cnn.com. The third time I made up a server and
executed java WhoIs made.up.com and since there is no such host (until someone registers
it!) and UnknownHostException
occurs and our code prints out the message
"Unknown Host Exception!"
.
Remember if your computer is not connected to the Internet you can use the loopback address
of 127.0.0.1
which is referred to as the hostname localhost
,
provided that you have installed a TCP stack to your computer. This is useful if you are working
at home and don't want to be billed by your ISP while you are developing networking applications.
© 2006
Dr. Derek Molloy
(DCU).