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

时间: 2024-10-11 01:17:33

InetAddress Example program in Java的相关文章

Eclipse通过Maven构建时出现: Fatal error compiling: tools.jar not found: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jre1.8.0_31\..\lib\tools.jar

错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project testtxannotation: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jre1.8.0_31\..\lib\tools.jar 这种错误是偶然性的,构建几次可能出现一次

在XP系统下搭建maven环境出的问题 Unable to locate the Javac Compiler in: C:\Program Files\Java\jre6\..\lib\tools.jar

Build errors for spider; org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project spider: Compilation failureUnable to locate the Javac Co

Java,AWTUtilities,eclipse报编译错误:Access restriction: The type &#39;AWTUtilities&#39; is not API (restriction on required library &#39;C:\Program Files\Java\jre7\lib\rt.jar&#39;)

[场景]调用com.sun.awt.AWTUtilities时,eclipse提示编译错误: Access restriction: The type 'AWTUtilities' is not API (restriction on required library 'C:\Program Files\Java\jre7\lib\rt.jar') [分析]eclipse项目默认的jre版本较早. com.sun.awt.AWTUtilities是在JDK 6 update10中增加的. [解决

Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar 报错

报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar   解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入. 将jre项目用自己下载的jre

Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\Program Files\Java\jre7

Maven编译时两则信息 (Workspace以及default classpath container) 博客分类: Java 使用Maven一年有余,却总是被两则不起眼的编译信息困扰,终想查明,这两则信息如下: Java代码   [INFO] Using Eclipse Workspace: D:\maven\workspace [WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C:\

【转】Error: no `server&#39; JVM at `C:\Program Files\Java\jre6\bin\server\jvm.dll&#39;.解决办法

出现问题: 用java -jar XXX.jar -server -Xms900m -Xmx900m 或者 java -server -version 提示错误 Error: no `server' JVM at `C:\Program Files\Java\jre6\bin\server\jvm.dll'.. 解决方法 copy ’server’ folder from the JDK’s JRE’s bin folder (example: C:\Program Files\Java\jdk

&quot;com.android.ide.s.ProcessException:Process &#39;cand &#39;C:\Program Files\Java\jdk1.8.0_60\bin\java.exe&#39;&#39; finished with non-zero exit value 2&quot;

使用Android Studio 出现该问题: "com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' finished with non-zero exit value 2" 导致原因:项目中出现了重复的依赖.删除依赖项便可

【Java】移动JDK路径后,修改环境变量不生效 Error: could not open `C:\Program Files\Java\jre1.8.0_131\lib\amd64\jvm.cfg&#39;

场景: JDK原先装在C盘的,现在移动到了D盘,并在环境变量修改了%JAVA_HOME%的新路径,但是CMD中输入java后依然报错. Error: could not open `C:\Program Files\Java\jre1.8.0_131\lib\amd64\jvm.cfg' 解决办法: 在环境变量PATH中把下面这段挪到PATH最前面即可.记得要关闭再打开CMD后再输入java检查. %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 再不行的话也许可以试试这

Error: could not open `C:\Program Files\Java\jre6\lib\i386\jvm.cfg&#39;

昨天刚过情人节!哈哈,好久没记录学习内容了,今天在房子闲着没事重新安装jdk,按道理很简单的; 第一步下载jdk; 第二步傻瓜式安装完; 第三步配置环境变量(下边根据自己安装时路径做相应变化) a.JAVA_HOME:"D:\Program Files\Java\jdk1.6.0_37;": b.Path:";D:\Program Files\Java\jdk1.6.0_37\bin;D:\Program Files\Java\jdk1.6.0_37\jre\bin;&quo