zoukankan      html  css  js  c++  java
  • 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".

  • 相关阅读:
    在美国贩卖早餐的小摊贩
    随感
    业内人士称游资3年前开始准备炒作糖价
    许志:量化宽松在即 美元迎来关键一周
    9月17日  逾200亿资金净流出 农行轰然长阴 好笑
    9月热钱流入环比加速 多为投机性资金
    错过了多次捞钱的机会
    20101012 期货盘面暴跌,亏损持仓
    致歉申明,现在《微电子工程师》可以正常下载了
    SemiId半导体型号识别器1.0发布
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4039360.html
Copyright © 2011-2022 走看看