实验3:Internet寻址(解答)

实验三 Internet 寻址一、实验目的1.2.3.4. 熟悉网络编程的基本过程 掌握网络程序的基本调试方法 学习InetAddress 类的应用 理解Internet 寻址的基本概念二、实验内容

实验三 Internet 寻址

一、实验目的

1.

2.

3.

4. 熟悉网络编程的基本过程 掌握网络程序的基本调试方法 学习InetAddress 类的应用 理解Internet 寻址的基本概念

二、实验内容

1.

2.

3. 输入、调试、运行一个简单的网络例程,熟悉网络编程的过程。 利用InetAddress 类编写一个能够获取本机地址的Java 程序。 利用InetAddress 类编写一个既能把域名解析为地址的程序,又能把地址解析为域名

的Java 程序。

三、实验步骤

1. 在Eclipse 中输入以下两个程序:

import java.io.*;

import java.net.*;

public class SimpleServer {

public static void main(String[] args) {

try {

ServerSocket ss=new ServerSocket(2007);

Socket s=ss.accept();

Reader in=new InputStreamReader(s.getInputStream());

int c;

while((c=in.read())!=-1){ System.out.write(c);}

} catch (IOException e) {e.printStackTrace();}

}

}

import java.net.*;

import java.io.*;

public class SimpleClient {

public static void main(String[] args) {

try {

Socket s=new Socket("localhost",2007);

Writer out=new OutputStreamWriter(s.getOutputStream());

out.write("Hello,Java network programming!!!n");

out.close();

} catch (UnknownHostException e) {e.printStackTrace(); }

catch (IOException e) {e.printStackTrace(); }

}

}

,

2. 按照先运行SimpleServer 后运行SimpleClient 的次序运行这两个程序,察看结果。

程序会在服务器端打印:Hello,Java network programming!!!

然后客户端程序与服务器端程序同时结束。

3. 然后先运行SimpleClient ,看看有什么结果。

客户端将出现: java.net.ConnectException : Connection refused: connect

at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at lab.lab3.SimpleClient.main(SimpleClient.java:8)

4.

5. 利用InetAddress 类编写一个能够获取本机地址的Java 程序,然后运行察看结果。 输入下面的程序:

import java.net.InetAddress;

/**

* A command-line utility for performing regular and reverse DNS lookups

*/

public class DNSResolver {

/**

* Expects single argument containing a domain name or dotted decimal

* IP address and outputs the domain-address mappings.

*/

public static void main(String[] args) {

// if (args.length != 1) {

,

boolean isReverseLookup = Character.isDigit(str.charAt(0));

try {

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

for(int a=0; a

InetAddress address = addresses[a];

if (isReverseLookup) {

if (address.getHostAddress().equals(address.getHostName())) {

System.out.println("Could not reverse resolve "

address.getHostAddress());

} else {

System.out.println(address.getHostAddress()

" reverse resolves to "

address.getHostName());

}

} else {

System.out.println(address.getHostName()

" resolves to "

address.getHostAddress());

}

}

} catch (java.net.UnknownHostException e) {

System.err.println("Cannot resolve " str);

System.exit(1);

} catch (java.lang.SecurityException e) {

System.err.println("A security manager has been installed and the"

" 'resolve' java.net.SocketPermission has not"

" been granted");

System.exit(1);

}

}

}

}

在Eclipse 中运行该程序,看看输入www.baidu.com 、www.microsoft.com 、127.0.0.1等值后,看看是什么结果。

结果分别是: www.sina.com.cn resolves to 218.30.66.101

www.microsoft.com resolves to 207.46.19.190

www.microsoft.com resolves to 207.46.193.254

www.microsoft.com resolves to 207.46.192.254

,

www.microsoft.com resolves to 207.46.19.254

127.0.0.1 reverse resolves to localhost

四、调试分析

在调试程序DNSResovler 时,常常会出现异常,后来经过仔细排查后发现机器没有连接上Internet ,从而导致程序抛出网络连接异常,网络连通后可以正确运行。

五、实验结果

通过上机,按照实验指导书操作,得到了题目所要求的实验结果。

六、实验总结

通过本次实验,我熟悉了网络编程的基本过程,掌握了网络程序的基本调试方法、InetAddress 类的应用方法,理解Internet 寻址的基本概念。

标签: