Obtaining Hostname and IP Address Information

   

Often when dealing with network applications, it's necessary to figure out the IP address of some remote host. The InetAddress class represents an Internet Protocol (IP) address.

The InetAddress class contains an Internet host address. Internet hosts are identified one of two ways:

  • Name

  • Address

We have already discussed how a name is associated with an IP address back in the section "TCP/IP Architecture."

As a refresher, when you make a connection to http://www.javasoft.com, your system needs to find out the numeric address for JavaSoft. It will use a service called Domain Name Service, or DNS.

You might have noticed that Internet host names are usually a number of names that are separated by periods. These separate names represent the domain a host belongs to. netcom5.netcom.com, for example, is the host name for a machine named netcom5 in the netcom.com domain. The netcom.com domain is a subdomain of the .com domain. A netcom.edu domain could be completely separate from the netcom.com domain, and netcom5.netcom.edu would be a totally different host. Again, this is not too different from phone numbers . The phone number 404-555-1017 has an area code of 404, for example, which could be considered the Atlanta domain. The exchange 555 is a subdomain of the Atlanta domain, and 1017 is a specific number in the 555 domain, which is part of the Atlanta domain. Just as you can have a netcom5.netcom.edu that is different from netcom5.netcom.com, you can have an identical phone number in a different area code, such as 212 “555 “1017.

The important point to remember here is that host names are only unique within a particular domain. Don't think that your organization is the only one in the world to have named its machines after The Three Stooges, Star Trek characters, or characters from various comic strips .

There are no constructors for the InetAddress class. If you need a new instance, you have to use one of the static methods on the class. The three ways to get an instance of the class from the InetAddress class itself are

 static InetAddress getByName(String host) static InetAddress[] getAllByName(String host) static InetAddress getLocalHost() 

The getByName method determines the IP address of a host using the host's name. The getAllByName method returns an array of InetAddress es if the host has multiple ones. One reason that the host might have multiple IPs is if there are multiple network cards on the host machine. The final method creates an instance of the InetAddress class for the localhost, which is the machine that the method is executed on. Listing 23.11 shows an example using all three methods.

Listing 23.11 Source Code for InetAddressExample.java
 import java.net.*; public class InetAddressExample {   // Default Constructor   public InetAddressExample()   {     super();   }   public void doExample()   {     try     {       // Use the getByName method       System.out.print( "Using getByName(): " );       System.out.println( InetAddress.getByName( "www.sun.com" ) );       // Use the getAllByName method       // This host is used because it has several IPs for this hostname       System.out.println( "Using getAllByName()" );       InetAddress[] addresses = InetAddress.getAllByName( "www.microsoft.com" );       int size = addresses.length;       for( int i = 0; i < size; i++ )       {         System.out.println( addresses[i] );       }       // Use the get LocalHost method       System.out.print( "Using getLocalHost(): " );       System.out.println( InetAddress.getLocalHost() );     }     catch( UnknownHostException ex )     {       ex.printStackTrace();     }   }   public static void main(String[] args)   {     InetAddressExample example = new InetAddressExample();     example.doExample();   } } 

Listing 23.12 shows the output from Listing 23.11.

Listing 23.12 Output from the InetAddressExample
 C:\jdk1.3se_book\classes>java InetAddressExample Using getByName(): www.sun.com/192.18.97.195 Using getAllByName() www.microsoft.com/207.46.230.219 www.microsoft.com/207.46.130.14 www.microsoft.com/207.46.130.149 www.microsoft.com/207.46.131.137 www.microsoft.com/207.46.230.218 Using getLocalHost(): ccavane01/24.92.158.120 C:\jdk1.3se_book\classes> 

You've seen how you can get an instance of the InetAddress class using the static methods on the class itself, but there other ways as well. On the Socket and ServerSocket classes, there are several methods that return an instance of the InetAddress class. Both the Socket and ServerSocket classes have a method called getInetAddress. This method on the ServerSocket class returns an address that represents the local address of the ServerSocket. The method on the Socket class returns the address that the Socket is connected to. As mentioned previously in this chapter, the InetAddress is a parameter to several of the constructors in both classes.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net