7.1 Java Internet Aware Applications

The InetAddress class

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:

NetApplication1.java

1234567891011121314151617181920212223
/* Java Network Application Example - Written by Derek Molloy for the EE402 Module
* See: ee402.eeng.dcu.ie
*/

package ee402;

import java.net.*;

public class NetApplication1 {

        public static void main(String[] args) {
                try{
                        InetAddress localAddress = InetAddress.getLocalHost();
                        String hostname = localAddress.getHostName();
                        String address = localAddress.getHostAddress();
                        System.out.println("Your host has name: " + hostname + " and address: " + address);
                }
                catch (UnknownHostException e){
                        System.out.println("Cannot detect localhost : " + e.toString());
                }
        }

}

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 executed by typing java ee402.NetApplication1. The output looks like Figure 7.1, “InetAddress Class Example”.

Figure 7.1 The InetAddress Class Example

In this case the local IP address is listed as 192.168.1.4 - and the Microsoft Networks name of my PC is DerekMolloy-PC. This is a fixed IP address behind my home firewall. The local hostname is extracted using the getHostName() method and returns the String object "DerekMolloy-PC".


A Simple "Who Is?" Application

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):

JavaWhoIs.java

12345678910111213141516171819202122232425
/* Java WhoIs Example - Written by Derek Molloy for the EE402 Module
* See: ee402.eeng.dcu.ie
*/

package ee402;

import java.net.*;

public class JavaWhoIs {

        public static void main(String[] args) {
                if (args.length!=1){
                        System.out.println("Usage is: WhoIs [hostname]");
                        System.exit(0);
                }
                try{
                        InetAddress i = InetAddress.getByName(args[0]);
                        System.out.println("The Address is: " + i.getHostAddress().toString());
                }
                catch(UnknownHostException e){
                        System.out.println("Unknown Host");
                }
        }
        
}

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 7.2, “Java WhoIs Example”)

Figure 7.2 The Java WhoIs Example

In Figure 7.2, “Java WhoIs Example” the first time I ran java ee402.JavaWhoIs I did not pass the hostname parameter, so I was prompted to do so. The second time I ran java ee402.JavaWhoIs dcu.ie and this worked well, returning the IP address 136.206.1.4

In the third case we called java ee402.JavaWhoIs rte.ie this returns 89.207.56.140. This could change each time you run the command as there would be several servers for www.rte.ie. The fourth time I made up a server and executed java ee402.JavaWhoIs made.up.com and since there is no such host (until someone registers it!) an UnknownHostException occurs and our code prints out the message "Unknown Host".

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. 


These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Comments