need immediately

Lab 5Repl: Looking Up Internet Addresses (NsLookup)/IPAddress Category


The java.net.InetAddress class is Java’s encapsulation of an IP address:

public final class InetAddress extends Object implements Serializable

This class represents an Internet address as two fields: hostname (a String) and address (an int). hostName contains the name of the host; for example, www.gannon.edu. address contains the 32-bit IP address.

  • Creating New InetAddress Objects: There are no public constructors in the InetAddress class. However, InetAddress has three static methods that return suitably initialized InetAddress objects, given a little information. They are:

public static InetAddress InetAddress.getByName(String hostName) throws UnknownHostException

public static InetAddress[] InetAddress.getAllByName(String hostName) throws UnknownHostException

public static InetAddress InetAddress.getLocalHost() throws UnknownHostException

  • All three of these may make a connection to the local DNS server to fill out the information in the InetAddress object.

  • A Program That Prints the Address of crispo.gannon.edu

import java.net.*;


public class OReillyByName {


public static void main (String[] args) {


try {

InetAddress address = InetAddress.getByName(“crispo.gannon.edu");

System.out.println(address);

}

catch (UnknownHostException e) {

System.out.println("Could not find crispo.gannon.edu ");

}

}

}

  • Here’s the result: crispo.gannon.edu/207.175.36.18


Getter Methods


The InetAddress class contains three getter methods that return the hostname as a string and the IP address as both a string and a byte array. These are:

public String getHostName()

public byte[] getAddress()

public String getHostAddress()


Example: Given the Address, Find the Hostname

import java.net.*;



public class ReverseTest {


public static void main (String args[]) {

try {

InetAddress ia = InetAddress.getByName("152.2.22.3");

System.out.println(ia.getHostName());

}

catch (Exception e) {

System.err.println(e);

}

}


public byte[] getAddress(): If you want to know the IP address of a machine, getAddress() returns an IP address as an array of bytes in network byte order. The most significant byte (i.e., the first byte in the address’s dotted quad form) is the first byte in the array, or element zero:

InetAddress me = InetAddress.getLocalHost();

byte[] address = me.getAddress();

The bytes returned are unsigned, which poses a problem. Bytes with values higher than 127 are treated as negative numbers. Therefore, if you want to do anything with the bytes returned by getAddress(), you need to promote the bytes to ints and make appropriate adjustments. Here’s one way to do it:

int unsignedByte = signedByte < 0 ? signedByte + 256 : signedByte;


Lab 5b Assignment (HostLookup/AddressTest): nslookup is a Unix utility that converts hostnames to IP addresses and IP addresses to hostnames. It has two modes: interactive and command line. If you enter a hostname on the command line, nslookup prints the IP address of that host. If you enter an IP address on the command line, nslookup prints the hostname. If no hostname or IP address is entered on the command line, nslookup enters interactive mode, in which it reads hostnames and IP addresses from input and echoes back the corresponding IP addresses or hostnames until you type “exit”.


Modify the nslookup functionality in this lab assignment such that your program also identifies the class or category of the IP address. That is, each time that you type in a hostname or IP address the program should not only display the corresponding IP address or hostname, it should also print out its IP address (network) class (A, B, C, or D).