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".
InetAddress Example program in Java的更多相关文章
- 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一年有余,却总是被两则不起眼的编译信息困扰,终想查明 ...
- 【转】Error: no `server' JVM at `C:\Program Files\Java\jre6\bin\server\jvm.dll'.解决办法
出现问题: 用java -jar XXX.jar -server -Xms900m -Xmx900m 或者 java -server -version 提示错误 Error: no `server' ...
- 在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 g ...
- C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
转载请标明出处:https://www.cnblogs.com/tangZH/p/10538982.html 今天,在项目过程中碰到了这个奇怪的问题,C:\Program Files\Java\jdk ...
- 【Java】移动JDK路径后,修改环境变量不生效 Error: could not open `C:\Program Files\Java\jre1.8.0_131\lib\amd64\jvm.cfg'
场景: JDK原先装在C盘的,现在移动到了D盘,并在环境变量修改了%JAVA_HOME%的新路径,但是CMD中输入java后依然报错. Error: could not open `C:\Progra ...
- Java,AWTUtilities,eclipse报编译错误:Access restriction: The type 'AWTUtilities' is not API (restriction on required library 'C:\Program Files\Java\jre7\lib\rt.jar')
[场景]调用com.sun.awt.AWTUtilities时,eclipse提示编译错误: Access restriction: The type 'AWTUtilities' is not AP ...
- 解决 Error: could not open `C:\Program Files\Java\jre7\lib\i386\jvm.cfg'
解决 Error: could not open `C:\Program Files\Java\jre7\lib\i386\jvm.cfg' 重装JDK后,因为没有装在以前的目录,运行java命令后报 ...
- Spring整合JUnit4进行AOP单元测试的时候,报:"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=64
错误代码 "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size= ...
- 解决 :java -version出现错误:“could not open `C:\Program Files\Java\jre7\lib\amd64\jvm.cfg”
cmd 下java -version出现错误:“could not open `C:\Program Files\Java\jre7\lib\amd64\jvm.cfg”,出现这种错误可能是由于先前有 ...
随机推荐
- 常用的工具GCC GDB Make Makefile
系统调用系统调用是操作系统提供给外部应用程序的一组特殊的接口.应用程序通过这组特殊“接口”来获得操作系统内核提供的服务.在 C 语言中,操作系统的系统调用通常通过函数调用的形式完成, 这是因为这些函数 ...
- IE6 png 透明 (三种解决方法)
FF和IE7已经直接支持透明的png图了,下面这个主要是解决IE6下透明PNG图片有灰底的 ====================================================== ...
- SqlServer日期(convert函数,getdate函数)
SqlServer日期(convert函数,getdate函数) 函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3.33毫秒 ...
- SQL效率低下原因主要有
类别 子类 表达式或描述 原因 索引 未建索引 产生全表扫描 未利用索引 substring(card_no,1,4)=′5378′ 产生全表扫描 amount/30< 1000 ...
- WebComponent
WebComponent 前言 最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:& ...
- 在mac系统安装Apache Tomcat的详细步骤
对于Apache Tomcat 估计很多童鞋都会,那么今天就简单说下在mac上进行tomcat的安装: 第一步:下载Tomcat 这里Himi下载的tomcat version:7. ...
- Python的数据类型总结
原地可变类型和不可变类型 原地不可变类型又叫可哈希(hashable)类型,原地可变类型又叫不可哈希类型. 原地不可变类型: 数字类型:int, float, decimal.Decimal, fra ...
- python中self.__class__
1. python中的self python中的self就相当于C++中的this指针也就是指向对象本身的指针self.name = name 就是当前对象的成员变量name赋值为name. 2.py ...
- JavaWeb学习总结(一)——JavaWeb开发入门(转载)
转载自http://www.cnblogs.com/xdp-gacl/p/3729033.html感谢孤傲苍狼的分享. 一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意 ...
- Linux用户环境变量
Linux用户环境变量 环境变量就是系统或软件设置的一些参数,用户环境变量就是用户登录系统后,都有自已专用的运行环境.在Windows系统中用户环境变量保存在用户家目录,Linux也是同样的.本文主要 ...