实验03 InetAddress文档格式.docx

上传人:b****3 文档编号:17693451 上传时间:2022-12-08 格式:DOCX 页数:16 大小:34.79KB
下载 相关 举报
实验03 InetAddress文档格式.docx_第1页
第1页 / 共16页
实验03 InetAddress文档格式.docx_第2页
第2页 / 共16页
实验03 InetAddress文档格式.docx_第3页
第3页 / 共16页
实验03 InetAddress文档格式.docx_第4页
第4页 / 共16页
实验03 InetAddress文档格式.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

实验03 InetAddress文档格式.docx

《实验03 InetAddress文档格式.docx》由会员分享,可在线阅读,更多相关《实验03 InetAddress文档格式.docx(16页珍藏版)》请在冰豆网上搜索。

实验03 InetAddress文档格式.docx

:

80/homepage/search.jsp?

Key=JAVA

为了方便程序员编程,JDK中提供了URL类,该类的全名是.URL,该类用于使用它的各种方法来对URL对象进行分割、合并等处理,如图2-2所示。

图1-2URL类结构

URL有6种构造方法,通常使用了绝对URL路径构造方法,其中的URL参数是一个完整的URL字符串,而且必须要包含传输协议,如:

URL 

raceHtml=newURL("

"

);

四、常用方法

1.InetAddress类主要方法

 

byte[]

getAddress()返回该对象的原始IP。

static 

InetAddress[]

getAllByName(String 

host)获得指定主机域名的所有IP地址。

InetAddress

getByAddress(byte[] 

addr)获得指定IP地址对象。

getByAddress(String 

host,byte[] 

addr)根据指定主机名和IP地址创建对象。

getByName(String 

host)根据指定主机名获得对象。

String

getCanonicalHostName()获得指定域名的法定信息。

getHostAddress()返回当前IP地址字符串。

getHostName()获得当前IP地址的主机名。

getLocalHost()获得本地主机。

boolean

isLoopbackAddress()判断IP是否为环回地址。

2.URL类主要方法

getAuthority()获得URL的认证部分。

Object

getContent()获得URL的内容。

getContent(Class[] 

classes)获得URL的内容

int

getDefaultPort()获得URL默认的端口号

getFile()获得URL的文件名

getHost()获得该URL的主机名

getPath()获得该URL的路径

getPort()获得该URL的端口

getProtocol()获得该URL的协议。

getQuery()获得该URL的查询部分

getRef()获得该URL的锚部分

getUserInfo()获得该URL的用户信息

URLConnection

openConnection()返回URL的连接。

InputStream

openStream()打开URL的输入读取流

四.概念填空

1.所有的公开的IP地址由国际组织(NIC)分配,全球有3个网络信息中心,分别是(InterNIC)、(ENIC)、(APNIC)。

2.在IPv4中,A类IP地址以0开头,B类IP地址以(128)开头,C类IP地址以(192)开头,D类IP地址用于(224),E类IP地址是(240)。

3.在IPv4地址日益枯竭的现状,国际IP管理组织推出了3组内部自由分配的IP地址,分别是A类(10.0.0.0—10.255.255.255),B类(172.16.0.0-172.31.255.255)和C类(192.168.0.0-192.168.255.255)。

4.InetAddress是InternetAddress的缩写,为了满足IPv4和IPv6的需求,它有2个子类,分别是(Inet4Address)和(Inet6Address)。

5.InetAddress类不能通过构造方法获得对象,只能通过(getByName())、(getByAddress(ip))、(getByAllName())、(getByAddress(主机名。

Ip))、()等获得InetAddress类对象。

6.URL的作用是(internet中对网络资源进行统一的定位和管理的标识),其标准格式为(<

访问方法>

//<

用户名>

<

密码>

@<

主机>

端口>

/<

路径>

[?

参数])。

7.中文简体编码为(GB2312),繁体中文编码为(BIG-5)。

8.利用JAVA语言如何获得系统信息(通过调用System类中getProperties()方法来得到当前操作系统的信息)。

9.在JAVA语言中,采用(String类)进行指定字符串的转换。

五.程序填空

1.基础InetAddress程序

(importjava.inet.*;

)//引用网络类库包

importjava.util.*;

classmyJava{

publicstaticvoidmain(String[]args){

(try){//异常处理

InetAddressinet=(InetAddress).getByName(“abc”);

//类名

System.out.println(inet.toString());

}catch(Exceptione){

System.err.println(“获得本地信息失败”_+e.toString()__________________);

//获得异常信息

}

}

2.扫描指定网段的主机情况

import.*;

StringnetString=“192.168.1.0”;

try{

for(inti=0;

i<

255;

i++){

InetAddressinet=InetAddress.getByAdress(netString+i);

System.err.println(e.getMessage());

六、实验内容

1.该例程使用InetAddress类,用于获得指定的计算机名称和IP地址,在该程序示例中,需要将各注释程序段一一测试运行。

classInetAddressDemo{

publicstaticvoidmain(String[]args)throwsUnknownHostException{

//练习一,获得本地主机信息

/*

InetAddresslocalAddress=InetAddress.getLocalHost();

System.out.println(localAddress);

*/

//练习二,获得指定域名主机的信息

Stringhost="

;

InetAddressaddress=InetAddress.getByName(host);

System.out.println(address);

//练习三,根据指定域名获得所有信息

//注意获得的所有IP地址

InetAddress[]addresses=InetAddress.getAllByName(host);

for(InetAddressaddress:

addresses)

System.out.println(address);

//练习四,比较根据localhost和计算机名获得信息的不同

localhost"

//更改localhost为你现在所使用计算机名,查看不同

InetAddressia=InetAddress.getByName(host);

System.out.println("

CanonicalHostName="

+ia.getCanonicalHostName());

HostAddress="

+ia.getHostAddress());

HostName="

+ia.getHostName());

IsLoopbackAddress="

+ia.isLoopbackAddress());

//练习五,获得本地主机所有IP地址

Enumeration<

NetworkInterface>

netInterfaces=null;

try{

netInterfaces=NetworkInterface.getNetworkInterfaces();

while(netInterfaces.hasMoreElements()){

NetworkInterfaceni=netInterfaces.nextElement();

System.out.println("

DisplayName:

+ni.getDisplayName());

Name:

+ni.getName());

InetAddress>

ips=ni.getInetAddresses();

while(ips.hasMoreElements()){

IP:

+ips.nextElement().getHostAddress());

}

}catch(Exceptione){

e.printStackTrace();

}

*/

//练习六,根据IP地址构造InetAddress

/*

byte[]ip=newbyte[]{(byte)202,(byte)117,(byte)128,7};

//可以更改数值超过255

InetAddressaddress1=InetAddress.getByAddress(ip);

InetAddressaddress2=InetAddress.getByAddress("

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

二.再执行

3.修改hosts文件名为hosts.txt,再执行第二步程序;

修改ip地址为888.888.888.888,再执行第二步程序;

在ip地址前添加空格“”或其它任意非数字字符。

再观察运行结果。

将运行结果截图

3.该例程采用了URL类,用于解析在单行编辑框中输入的URL地址,分解出访问协议、主机名称、端口号以及文件路径名称等。

import.URL;

import.MalformedURLException;

classmyURL{

URLurl;

publicmyURL(StringurlStr){

try{

url=newURL(urlStr);

}catch(Exceptione){

System.err.println(e.getMessage());

}

publicvoidresolve(){

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());

myURLmy=

newmyURL("

http:

//222.24.16.1:

8080/home/index.jsp#new?

key=java"

my.resolve();

}}

//图形化的URL输入和分析

importjava.awt.*;

importjava.awt.event.*;

classgetURLInformationextendsFrame{

TextFieldtUrl=newTextField("

输入URL地址"

ListlItems=newList(20);

ButtonbOk=newButton("

解析"

Fontf=newFont("

Serif"

Font.BOLD,30);

publicgetURLInformation(){

this.setLayout(newBorderLayout());

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(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

dispose();

System.exit(0);

}

});

bOk.addActionListener(newListener());

publicstaticvoidmain(Stringargs[]){

System.out.println("

StartingNew..."

getURLInformationmainFrame=newgetURLInformation();

mainFrame.setSize(400,400);

mainFrame.setTitle("

解析URL"

mainFrame.setVisible(true);

classListenerimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

URLgetURL=null;

try{

getURL=newURL(tUrl.getText());

}catch(MalformedURLExceptione1){

System.out.println("

URL解析错误"

+e1);

lItems.add("

Reference:

+getURL.getRef(),0);

FileName:

+getURL.getFile(),0);

FilePath:

+getURL.getPath(),0);

HostPort:

+getURL.getPort(),0);

HostName:

+getURL.getHost(),0);

Protocol:

+getURL.getProtocol(),0);

}}}

在输入URL时,分别更改以下内容,分别记录运行结果:

1)缺省或笔误访问协议,例如输入URL:

或htp:

//

2)使各部分间隔符号残缺或误用,例如输入URL:

/或http/:

/index.html

3)修改端口号超过65535

4.通过URL获得网页上文本和图像资源

//获得文本资源

importjava.io.*;

classexp_4_7{

publicstaticvoidmain(String[]args)throwsException{

Stringurl="

填入任意网页URL"

//自行输入URL,例如;

InputStreamin=(newURL(url)).openStream();

//获得指定URL的字节输入流

intc=-1;

while((c=in.read())!

=-1){//按字节的方式输入数据和输出数据

System.out.write(c);

//获

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1