InetAddress Example program in Java

The InetAddress class has no visible constructors. To create an InetAddress object, you have to use one of the available factory methods. Factory methods are merely a convention whereby static methods in a class return an instance of that class. This is done in lieu of overloading a constructor with various parameter lists when having unique method names makes the results much clearer. In the case of InetAddress, the three methods getLocalHost()getByName(), and getAllByName() can be used to create instances of InetAddress. These methods are shown here:

static InetAddress getLocalHost( ) 
throws UnknownHostException 
static InetAddress getByName(String hostName) 
throws UnknownHostException 
static InetAddress[ ] getAllByName(String hostName) 
throws UnknownHostException

The getLocalHost( ) method simply returns the InetAddress object that represents the local host. The getByName( ) method returns an InetAddress for a host name passed to it. If these methods are unable to resolve the host name, they throw an UnknownHostException.

On the Internet, it is common for a single name to be used to represent several machines. In the world of web servers, this is one way to provide some degree of scaling. The getAllByName( ) factory method returns an array of InetAddresses that represent all of the addresses that a particular name resolves to. It will also throw an UnknownHostException if it can't resolve the name to at least one address.

The following example prints the addresses and names of the local machine and two well-known Internet web sites:

package org.javalobby.tnt.net;

// Demonstrate InetAddress. 
import java.net.*;

public class InetAddressTest {
    public static void main(String args[]) throws UnknownHostException {
        InetAddress Address = InetAddress.getLocalHost();
        System.out.println(Address);
        Address = InetAddress.getByName("starwave.com");
        System.out.println(Address);
        InetAddress SW[] = InetAddress.getAllByName("www.baidu.com");
        for (int i = 0; i < SW.length; i++)
            System.out.println(SW[i]);
        Address = InetAddress.getByName("www.xxdhngx.com");
    }
}

Here is the output produced by this program. (Of course, the output you see will be slightly different.)

default/206.148.209.138 
starwave.com/199.181.132.250
www.baidu.com/180.97.33.108
www.baidu.com/180.97.33.107
www.baidu.com/61.135.169.125
www.baidu.com/61.135.169.121
Exception in thread "main" java.net.UnknownHostException: www.xxdhngx.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1246)
    at java.net.InetAddress.getAllByName(InetAddress.java:1162)
    at java.net.InetAddress.getAllByName(InetAddress.java:1098)
    at java.net.InetAddress.getByName(InetAddress.java:1048)
    at org.javalobby.tnt.net.InetAddressTest.main(InetAddressTest.java:15)

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".

原文地址:https://www.cnblogs.com/ghgyj/p/4039360.html