計算機(jī)網(wǎng)絡(luò)第二次作業(yè)

題目要求:

1.編程作業(yè),要求用一種自己擅長的語言實現(xiàn)ip地址的獲取,及其詳細(xì)信息的顯示,要求用窗口顯示。

2.生成GIF動態(tài)演示圖片,要求先演示程序目錄結(jié)構(gòu)與內(nèi)容,然后以程序源碼為背景,演示軟件運行過程。

3.最后提交的軟件以綠色軟件的形式提交,具體為兩種形式:

--a)壓縮文件:運行文件(如nicinfo.exe)+ 各種運行所需要文件(如xxx.dll)夠成的壓縮包,解壓可以直接運行,不需要安裝任何軟件或插件;

--b)單一運行文件:利用相關(guān)工具將原有的可執(zhí)行程序和所需要各種資源文件打包成一個可直接運行于Windows或Linux平臺的單一文件。

一.編寫函數(shù)

我采用的語言是java,由于涉及到窗口的創(chuàng)建,所以java書第九章的知識很重要,把其中的關(guān)鍵要點掌握下來。又由于要獲取主機(jī)信息和ip信息,這方面的知識不會,只有求助于百度了。完成情況基本如下:

代碼:

main.java

package IP1;

/**

* @author JavaAlpha

* @date 2011-12-14

* @version V 1.0 java代碼 調(diào)用dos的ipconfig /all 命令,獲取網(wǎng)卡詳細(xì)信息

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import java.io

.BufferedReader;

import java.io

.IOException;

import java.io

.InputStreamReader;

public class main {

public static void main(String[] args){

String hostName = getLocalMachineInfo("主機(jī)名? . . . . . . . . . . . . . :");

JFrame f=new JFrame("網(wǎng)卡信息");

Label la=new Label("用戶名稱;");

la.setBounds(10, 10, 100, 30);

la.setFont(new Font("宋體", Font.BOLD,20));

f.add(la);

JComboBox comboBox=new JComboBox();

comboBox.addItem("請選擇用戶");

comboBox.addItem(hostName);

comboBox.setBounds(115, 10, 200, 30);

comboBox.setFont(new Font("宋體", Font.BOLD,20));

f.add(comboBox);

JButton button=new JButton("詳細(xì)");

button.setFont(new Font("宋體", Font.BOLD,20));

button.setBounds(320,10,90,30);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

String item=(String)comboBox.getSelectedItem();

if(hostName.equals(item)){

new xiangxi();}

}

});

f.add(button);

f.setLayout(null);

f.setSize(500,300);

f.setLocation(300,200);

f.setVisible(true);

}

static String getLocalMachineInfo(String str) {

String line = "";

int n;

try {

Process ps = Runtime.getRuntime().exec("cmd /c ipconfig /all");

BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

while (null != (line = br.readLine())) {

if (line.indexOf(str) != -1) { // 在line字符串中 查找str出現(xiàn)的位置,str即上面的字符串

n = line.indexOf(":");

line = line.substring(n + 2); // 獲得 :后面的數(shù)據(jù);

break;

}

}

ps.waitFor();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return line; // 返回數(shù)據(jù)

}

}


1.1
1.2

xiangxi.java

package IP1;

import java.awt.*;

import java.awt.event.*;

import javax.swing.JFrame;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class xiangxi {

public xiangxi()

{

// 獲取機(jī)器名

String hostName = getLocalMachineInfo("主機(jī)名? . . . . . . . . . . . . . :");

// 獲取網(wǎng)卡型號

String desc = getLocalMachineInfo("描述. . . . . . . . . . . . . . . :");

// 獲取MAC地址

String mac = getLocalMachineInfo(" 物理地址. . . . . . . . . . . . . :");

// 獲取IP地址

String ip = getLocalMachineInfo(" IPv4 地址 . . . . . . . . . . . . :");

// 獲取子網(wǎng)掩碼

String subnetMask = getLocalMachineInfo(" 子網(wǎng)掩碼? . . . . . . . . . . . . :");

// 獲取默認(rèn)網(wǎng)關(guān)

String defaultGateway = getLocalMachineInfo(" 默認(rèn)網(wǎng)關(guān). . . . . . . . . . . . . :");

// 獲取DNS服務(wù)器

String DNSServers = getLocalMachineInfo("DNS 服務(wù)器? . . . . . . . . . . . :");

JFrame f = new JFrame("ip信息");

Label la = new Label("機(jī)器名:" + hostName);

la.setBounds(10, 10, 500, 30);

la.setFont(new Font("宋體", Font.BOLD,20));

f.add(la);

Label lb = new Label("網(wǎng)卡信息:" + desc);

lb.setBounds(10, 50, 500, 30);

lb.setFont(new Font("宋體", Font.BOLD,20));

f.add(lb);

Label lc = new Label("MAC地址:" + mac);

lc.setBounds(10, 90, 500, 30);

lc.setFont(new Font("宋體", Font.BOLD,20));

f.add(lc);

Label ld = new Label("IP地址:" + ip);

ld.setBounds(10, 130, 500, 30);

ld.setFont(new Font("宋體", Font.BOLD,20));

f.add(ld);

Label le = new Label("子網(wǎng)掩碼:" + subnetMask);

le.setBounds(10, 170, 500, 30);

le.setFont(new Font("宋體", Font.BOLD,20));

f.add(le);

Label lf = new Label("默認(rèn)網(wǎng)關(guān):" + defaultGateway);

lf.setBounds(10, 210, 500, 30);

lf.setFont(new Font("宋體", Font.BOLD,20));

f.add(lf);

Label lg = new Label("Dns服務(wù)器:" + DNSServers);

lg.setBounds(10, 250, 500, 30);

lg.setFont(new Font("宋體", Font.BOLD,20));

f.add(lg);

f.setLayout(null);

f.setSize(500, 400);

f.setLocation(300, 200);

f.setVisible(true);

}

static String getLocalMachineInfo(String str) {

String line = "";

int n;

try {

Process ps = Runtime.getRuntime().exec("cmd /c ipconfig /all");

BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));

while (null != (line = br.readLine())) {

if (line.indexOf(str) != -1) { // 在line字符串中

// 查找str出現(xiàn)的位置,str即上面的字符串

n = line.indexOf(":");

line = line.substring(n + 2); // 獲得 :后面的數(shù)據(jù);

break;

}

}

ps.waitFor();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

return line; // 返回數(shù)據(jù)

}

}


1.3


二,心得與體會

? ? ? 本次作業(yè)技術(shù)難度有點高,對于知識的掌握比較細(xì),使我對于Java圖形用戶界面的知識掌握加深,也讓我了解了關(guān)于訪問系統(tǒng)信息的相關(guān)知識。相信每一次的作業(yè)都能讓我取得進(jìn)步。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容