实验三InetAddress类和URL类
一.本实验目的及要求:
1.熟练掌握IPv4地址分类和域名的概念;
2.熟练掌握InetAddress类和其生成InetAddress对象的方法(getLocalHost(),getByName(),getAllByName(),getByAddress()等);
3.熟练掌握URL类和其方法(getProtocol(),getHost(),getPort(),getFile(),getRef()等),可对指定的URL地址进行解析;
4.掌握使用System.getProperties()获得JVM系统信息;
5.利用new String(str Value.getBytes(“ISO8859_1”), “GBK”)构造方法,实现指定字符集之间的转换。
6.实验报告内容应包括,本实验的第三、四、五部分的答案,以及第六部分的程序后要求。二、基础知识
1.InetAddress类
在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换。如图1-1所示。
图2-2 InetAddress类结构
由于InetAddress类只有一个构造函数,且不能传递参数,所以不能直接创建该对象实例,比如下面的做法就是错误的:
InetAddress ia = new InetAddress (); ×
可通过以下5个成员方法获得InetAddress对象或InetAddress数组:
getAllByName(String host)方法返回一个InetAddress对象的引用,每个对象包含一个表示相应主机名的单独的IP地址,这个IP地址是通过host参数传递的,例如:
InetAddress [] ia = getAllByName(“MyHost”);
●getByAddress(byte [] addr)方法返回一个InetAddress对象的引用,这个对象包含了
一个Ipv4地址或Ipv6地址,Ipv4地址是一个4字节数组,Ipv6地址是一个16字节地
址数组。
●getByAddress(String host, byte [] addr)方法返回一个InetAddress对象的引用,这
个InetAddress对象包含了一个由host和4字节的addr数组指定的IP地址,或者是host
和16字节的addr数组指定的IP地址。
●getByName(String host)方法返回一个InetAddress对象,该对象包含了一个与host参
数指定的主机相对应的IP地址。
●getLocalHost()方法返回一个InetAddress对象,这个对象包含了本地机的IP地址。
以上各方法均可能产生的UnknownHostException(未知的主机名)异常。当获得了InetAddress 类对象的引用就可以调用InetAddress的各种方法来获得InetAddress子类对象中的IP地址信息。例,通过调用getCanonicalHostName()从域名服务中获得标准的主机名,getHostAddress()获得IP地址,getHostName()获得主机名,isLoopbackAddress()判断IP地址是否是一个loopback环回地址。
2.URL类
IP地址唯一标识了Internet上的计算机,而URL则标识了这些计算机上的资源。通常,URL 是一个包含了传输协议、主机名称、服务端口号、文件路径名称,以及间隔符号“://”、“:”、“/”等信息的字符串,例:
https://www.wendangku.net/doc/1a2560861.html,:80/homepage/search.jsp?Key=JAVA
为了方便程序员编程,JDK中提供了URL类,该类的全名是https://www.wendangku.net/doc/1a2560861.html,.URL,该类用于使用它的各种方法来对URL对象进行分割、合并等处理,如图2-2所示。
图1-2 URL类结构
URL有6种构造方法,通常使用了绝对URL路径构造方法,其中的URL参数是一个完整的URL 字符串,而且必须要包含传输协议,如:
URL raceHtml=new URL("https://www.wendangku.net/doc/1a2560861.html,");
四、常用方法
1.InetAddress类主要方法
2.URL类主要方法
四.概念填空
1. 所有的公开的IP地址由国际组织(NIC )分配,全球有3个网络信息中心,分别是(InterNIC )、(ENIC )、(APNIC )。
2. 在IPv4中,A类IP地址以0开头,B类IP地址以(128 )开头,C类IP地址以(192 )开头,D类IP地址用于(组播地址),E类IP地址是(仅供试验的地址,保留)。
3. 在IPv4地址日益枯竭的现状,国际IP管理组织推出了3组内部自由分配的IP地址,分别是A 类(10.0.0.1 ~ 10.255.255.255 ),B类(172.16.0.0 ~
172.31.255.255 )和C类(192.168.0.0 ~ 192.168.255.255 )。
4. InetAddress是Internet Address的缩写,为了满足IPv4和IPv6的需求,它有2个子类,分别是(Inet4Address )和(Inet6Address )。
5. InetAddress类不能通过构造方法获得对象,只能通过(getLocalHost() )、(getByName() )、(getAllByName() )、(getByAddress() )、(getHostName() )等获得InetAddress类对象。
6. URL的作用是(Internet中对网络资源进行统一定位和管理的标识),其标准格式为(<访问方法>://<用户名>:<密码>@<主机>:<端口>/<路径>[?参数] )。
7. 中文简体编码为(GB 2312 ),繁体中文编码为(BIG 5 )。
8. 利用JA V A语言如何获得系统信息(通过调用System类中getProperties()方法来得到当前操作系统的信息)。
9. 在JA V A语言中,采用(String类中提供了字符编码转换的方法)进行指定字符串的转换。
五.程序填空
六、实验内容
1.该例程使用InetAddress类,用于获得指定的计算机名称和IP地址,在该程序示例中,需要将各注释程序段一一测试运行。
import https://www.wendangku.net/doc/1a2560861.html,.*;
import java.util.*;
class InetAddressDemo{
p ublic static void main (String [] args) throws UnknownHostException{
//练习一,获得本地主机信息
/*
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println(localAddress);
*/
//练习二,获得指定域名主机的信息
/*
String host = "https://www.wendangku.net/doc/1a2560861.html,";
InetAddress address = InetAddress.getByName(host);
System.out.println(address);
*/
//练习三,根据指定域名获得所有信息
/*
String host = "https://www.wendangku.net/doc/1a2560861.html,"; //注意获得的所有IP地址
InetAddress [] addresses = InetAddress.getAllByName(host);
for(InetAddress address : addresses)
System.out.println(address);
*/
//练习四,比较根据localhost和计算机名获得信息的不同
String host = "localhost"; //更改localhost为你现在所使用计算机名,查看不同
InetAddress ia = InetAddress.getByName (host);
System.out.println ("Canonical Host Name = " + ia.getCanonicalHostName ());
System.out.println ("Host Address = " + ia.getHostAddress ());
System.out.println ("Host Name = " + ia.getHostName ());
System.out.println ("Is Loopback Address = " + ia.isLoopbackAddress ());
//练习五,获得本地主机所有IP地址
/*
Enumeration
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration
while (ips.hasMoreElements()) {
System.out.println("IP:" + ips.nextElement().getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}
*/
//练习六,根据IP地址构造InetAddress
/*
byte [] ip = new byte[] { (byte) 202, (byte) 117, (byte)128 , 7}; //可以更改数值超过255
InetAddress address1 = InetAddress.getByAddress(ip);
InetAddress address2 = InetAddress.getByAddress("https://www.wendangku.net/doc/1a2560861.html,", ip);
System.out.println(address1);
System.out.println(address2);
*/
} } }
报告内容:将以上程序段分别取消注释和运行,并将运行结果截图
使用InetAddress类获得本校或计算机学院的WWW、FTP、MAIL等服务器信息。2.向Windows系统的hosts文件中注册一个域名和IP解析,再使用getByName()方法查看结果。
一.找到:C:\WINDOWS\system32\drivers\etc\hosts
在该文件中添加一行域名与IP的对应行,例如:
192.168.18.100 https://www.wendangku.net/doc/1a2560861.html,
二.再执行
import https://www.wendangku.net/doc/1a2560861.html,.*;
import java.util.*;
class InetAddressDemo{
p ublic static void main (String [] args) throws UnknownHostException{
String host = "https://www.wendangku.net/doc/1a2560861.html,";
InetAddress address = InetAddress.getByName(host);
System.out.println(address);
}
}
三.修改hosts文件名为hosts.txt,再执行第二步程序;修改ip地址为888.888.888.888,再执行第二步程序;在ip地址前添加空格“”或其它任意非数字字符。再观察运行结果。
报告内容:将运行结果截图
3.该例程采用了URL类,用于解析在单行编辑框中输入的URL地址,分解出访问协议、主机名称、端口号以及文件路径名称等。
import https://www.wendangku.net/doc/1a2560861.html,.URL;
import https://www.wendangku.net/doc/1a2560861.html,.MalformedURLException;
class myURL{
URL url;
public myURL(String urlStr){
try{
url = new URL(urlStr);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
public void resolve(){
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
System.out.println(url.getRef());
}
public static void main(String [] args){
myURL my =
new myURL("http://222.24.16.1:8080/home/index.jsp#new?key=java");
my.resolve();
} }
报告内容:将运行结果截图
//图形化的URL输入和分析
import https://www.wendangku.net/doc/1a2560861.html,.URL;
import https://www.wendangku.net/doc/1a2560861.html,.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
class getURLInformation extends Frame{
TextField tUrl = new TextField("输入URL地址");
List lItems = new List(20);
Button bOk = new Button("解析");
Font f = new Font("Serif",Font.BOLD,30);
public getURLInformation(){
this.setLayout(new BorderLayout());
this.add(tUrl, BorderLayout.NORTH);
this.add(lItems, BorderLayout.CENTER);
this.add(bOk, BorderLayout.SOUTH);
tUrl.setFont(f);
lItems.setFont(f);
bOk.setFont(f);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
bOk.addActionListener(new Listener());
}
public static void main(String args[]){
System.out.println("Starting New...");
getURLInformation mainFrame = new getURLInformation();
mainFrame.setSize(400, 400);
mainFrame.setTitle("解析URL");
mainFrame.setVisible(true);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
URL getURL = null;
try{
getURL = new URL(tUrl.getText());
}catch(MalformedURLException e1){
System.out.println("URL解析错误" + e1);
}
lItems.add("Reference:" + getURL.getRef(), 0);
lItems.add("File Name:" + getURL.getFile(), 0);
lItems.add("File Path:" + getURL.getPath(), 0);
lItems.add("Host Port:" + getURL.getPort(), 0);
lItems.add("Host Name:" + getURL.getHost(), 0);
lItems.add("Protocol :" + getURL.getProtocol(), 0);
} } }
报告内容:在输入URL时,分别更改以下内容,分别记录运行结果:
1)缺省或笔误访问协议,例如输入URL:https://www.wendangku.net/doc/1a2560861.html, 或htp://https://www.wendangku.net/doc/1a2560861.html,
2)使各部分间隔符号残缺或误用,例如输入URL:http:/https://www.wendangku.net/doc/1a2560861.html, 或http/https://www.wendangku.net/doc/1a2560861.html,:/index.html
3)修改端口号超过65535
5.获得JVM系统信息,记录在当前运行该程序的JVM信息
import java.io.*;
import java.util.*;
class showSYSCode{
public static void main(String [] args){
System.getProperties().list(System.out);
}
}
报告内容:对乱码和处理部分截图
记录以下信息:
1)sun.boot.library.path
2)java.vm.version
3)user.country
4)user.dir
5)user.home
6)https://www.wendangku.net/doc/1a2560861.html,
7)file.encoding
8)java.library.path
9)java.home
10)sun.desktop
6.本例程利用URL类型实现从指定地址获得文本格式的HTML源文件,注意:汉字乱码问题。
import java.io.*;
import https://www.wendangku.net/doc/1a2560861.html,.URL;
import https://www.wendangku.net/doc/1a2560861.html,.MalformedURLException;
import java.awt.*;
import java.awt.event.*;
class readURL extends Frame{
TextField tUrl = new TextField("输入URL地址");
List lItems = new List(20);
Button bOk = new Button("下载");
Font f = new Font("Serif",Font.BOLD,30);
public readURL(){
this.setLayout(new BorderLayout());
this.add(tUrl, BorderLayout.NORTH);
this.add(lItems, BorderLayout.CENTER);
this.add(bOk, BorderLayout.SOUTH);
tUrl.setFont(f);
lItems.setFont(f);
bOk.setFont(f);
//内部类
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
bOk.addActionListener(new Listener());
}
public static void main(String args[]){
System.out.println("Starting New...");
readURL mainFrame = new readURL();
mainFrame.setSize(400, 400);
mainFrame.setTitle("下载URL");
mainFrame.setVisible(true);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
URL getURL = null;
InputStream in = null;
try{
getURL = new URL(tUrl.getText());
in = getURL.openStream();
DataInputStream buffer = new DataInputStream(in);
String lineOfData, value;
int i=0;
while((lineOfData = buffer.readLine()) != null){
//value = new String(lineOfData.getBytes("ISO8859_1"),"GBK");
lItems.add((++i)+":"+lineOfData);
//lItems.add((++i)+":"+value); //注意这两句注释语句的作用}
}catch(MalformedURLException e1){
System.out.println("URL解析错误" + e1);
}catch(IOException e2){
System.out.println("IO错误" + e2);
}finally{
if( in != null){
try{
in.close();
lItems.add("下载完成");
}catch(IOException e3){
}
}
} } } }
报告内容:对乱码和处理部分截图
七.编写程序
1)实现利用InetAddress.getByName()按计算机名称获得实验室局域网中所有开机主机名称和IP 地址
2)实现利用InetAddress.getByName()按IP地址获得指定网络段所有开机主机名称/域名和IP地址,结合方法isReachable()