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".

  • 相关阅读:
    Java基础课程---将一个字符串反转,将字符串中指定部分进行反转,比如,"abcdefg", 反转为"abfedcg"
    在Centon64位中卸载安装时系统自带的--openjdk,并且安装自己需要的jdk
    Java基础课程---sleep()方法 和 wait()方法的异同(面试题)
    Java基础课程---权限修饰符
    Java基础课程---
    MySQL 创建库
    brew
    android webview load 本地文件需要注意的地方
    android下隐藏标题栏
    android webview无法加载网页
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4039360.html
Copyright © 2011-2022 走看看