實驗3:Internet尋址(解答)
實驗三 Internet 尋址一、實驗?zāi)康?.2.3.4. 熟悉網(wǎng)絡(luò)編程的基本過程 掌握網(wǎng)絡(luò)程序的基本調(diào)試方法 學(xué)習(xí)InetAddress 類的應(yīng)用 理解Internet 尋址的基本概念二、實驗內(nèi)容
實驗三 Internet 尋址
一、實驗?zāi)康?/p>
1.
2.
3.
4. 熟悉網(wǎng)絡(luò)編程的基本過程 掌握網(wǎng)絡(luò)程序的基本調(diào)試方法 學(xué)習(xí)InetAddress 類的應(yīng)用 理解Internet 尋址的基本概念
二、實驗內(nèi)容
1.
2.
3. 輸入、調(diào)試、運行一個簡單的網(wǎng)絡(luò)例程,熟悉網(wǎng)絡(luò)編程的過程。 利用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 的次序運行這兩個程序,察看結(jié)果。
程序會在服務(wù)器端打?。篐ello,Java network programming!!!
然后客戶端程序與服務(wù)器端程序同時結(jié)束。
3. 然后先運行SimpleClient ,看看有什么結(jié)果。
客戶端將出現(xiàn): 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.
4.
5. 利用InetAddress 類編寫一個能夠獲取本機地址的Java 程序,然后運行察看結(jié)果。 輸入下面的程序:
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等值后,看看是什么結(jié)果。 結(jié)果分別是: 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 四、調(diào)試分析 在調(diào)試程序DNSResovler 時,常常會出現(xiàn)異常,后來經(jīng)過仔細(xì)排查后發(fā)現(xiàn)機器沒有連接上Internet ,從而導(dǎo)致程序拋出網(wǎng)絡(luò)連接異常,網(wǎng)絡(luò)連通后可以正確運行。 五、實驗結(jié)果 通過上機,按照實驗指導(dǎo)書操作,得到了題目所要求的實驗結(jié)果。 六、實驗總結(jié) 通過本次實驗,我熟悉了網(wǎng)絡(luò)編程的基本過程,掌握了網(wǎng)絡(luò)程序的基本調(diào)試方法、InetAddress 類的應(yīng)用方法,理解Internet 尋址的基本概念。