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”,出现这种错误可能是由于先前有 ...
随机推荐
- 初用jquery
---恢复内容开始--- 这两天在顶顶大人的指导下,利用jquery框架做了一个动态切换的小页面.最终效果图: 这么萌萌哒的图片让我觉得一直在测试也没那么累.实现功能如下: 1.打开页面时,自动切换, ...
- c# winform textbox与combox让用户不能输入
textbox的ReadOnly属性设置为true combox的Enable属性设置为false 运行后效果如下 点击第一个和第二个,会把按钮text赋值给文本框和combox 并且用户不能输入
- PHP数组操作——获取数组最后一个值的方法
php开发过程中,可能经常需要对取出的数组要获取数组的最后健或值.在这里总结了三个方法,并且跟据他们三个方法在一些情况下如何使用的条件限制进行了说明. <?php $array=array(1, ...
- mysql开启日志记录慢查询
1.查看mysql配置 2.利用set (variables)命令设置变量 set global log_slow_queries = ON;/*(必须带上global)*/ set global s ...
- UIPageControll - 图片格式
设置pageCon的显示风格: 1. 颜色 page.pageIndicatorTintColor = [UIColor redColor]; page.currentPageIndicatorTin ...
- app应用程序本地化--备用
一.简介 * 使用本地化功能,可以轻松地将应用程序翻译成多种语言,甚至可以翻译成同一语言的多种方言 * 如果要添加本地化功能,需要为每种支持的语言创建一个子目录,称为”本地化文件夹”,通常使用.lpr ...
- XMLHttpRequest 使用概括
***********************************************XMLHttpRequest对象初始化:********************************* ...
- apache开启.htaccess及.htaccess的使用方法
今天本地调试PHP程序,用到了.htaccess,而默认配置里面开启.htaccess,在网上找到了开启.htaccess的可行方法,供朋友们借鉴.(开启的我他的方法不行,查找了一下AllowOver ...
- 机器学习的数学基础(1)--Dirichlet分布
机器学习的数学基础(1)--Dirichlet分布 这一系列(机器学习的数学基础)主要包括目前学习过程中回过头复习的基础数学知识的总结. 基础知识:conjugate priors共轭先验 共轭先验是 ...
- 【 HDU 1255】 覆盖的面积(矩阵面积交,线段树,扫描法)
[题目] 覆盖的面积 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100 ...