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

題目:寫一個實現(xiàn)ping的gui小程序。

代碼:


package ping;

import java.awt.Font;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JTextArea;

public class ping {

public static long time =0;

public static String[] l=null;

public static boolean ping(String ipAddress) throws Exception {

long starttime = System.currentTimeMillis();

int? timeOut =? 3000 ;? //超時應(yīng)該在3鈔以上

boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);? ? // 當(dāng)返回值是true時,說明host是可用的,false則不可。

long endtime = System.currentTimeMillis();

time=endtime-starttime;

System.out.println(endtime-starttime);

return status;

}

public static void ping02(String ipAddress) throws Exception {

String line = null;

try {

Process pro = Runtime.getRuntime().exec("ping " + ipAddress);

BufferedReader buf = new BufferedReader(new InputStreamReader(

pro.getInputStream(),"GBK"));

while ((line = buf.readLine()) != null)? {

System.out.println(line);

textArea.append(line + "\n");

}

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

}

public static boolean ping(String ipAddress, int pingTimes, int timeOut) {

BufferedReader in = null;

Runtime r = Runtime.getRuntime();? // 將要執(zhí)行的ping命令,此命令是windows格式的命令

String pingCommand = "ping " + ipAddress + " -n " + pingTimes? ? + " -w " + timeOut;

try {? // 執(zhí)行命令并獲取輸出

System.out.println(pingCommand);

Process p = r.exec(pingCommand);

if (p == null) {

return false;

}

in = new BufferedReader(new InputStreamReader(p.getInputStream(),"UTF-8"));? // 逐行檢查輸出,計算類似出現(xiàn)=23ms TTL=62字樣的次數(shù)

int connectedCount = 0;

String line = null;

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

connectedCount += getCheckResult(line);

}? // 如果出現(xiàn)類似=23ms TTL=62這樣的字樣,出現(xiàn)的次數(shù)=測試次數(shù)則返回真

return connectedCount == pingTimes;

} catch (Exception ex) {

ex.printStackTrace();? // 出現(xiàn)異常則返回假

return false;

} finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

//若line含有=18ms TTL=16字樣,說明已經(jīng)ping通,返回1,否則返回0.

private static int getCheckResult(String line) {? // System.out.println("控制臺輸出的結(jié)果為:"+line);

Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",? ? Pattern.CASE_INSENSITIVE);

Matcher matcher = pattern.matcher(line);

while (matcher.find()) {

return 1;

}

return 0;

}

public static JTextArea textArea;

@SuppressWarnings("unchecked")

public static void main(String[] args) throws Exception {

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

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

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

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

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

f.add(la);

Label la2=new Label(hostName);

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

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

f.add(la2);

String ipAddress = "127.0.0.1";

Label la1 = new Label("ping " + ipAddress);

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

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

f.add(la1);

Label lb = new Label("時間:" + ping.time);

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

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

f.add(lb);

textArea = new JTextArea();

textArea.setBounds(10,120,600,600);

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

f.add(textArea);

f.setLayout(null);

f.setSize(600,600);

f.setLocation(300,200);

f.setVisible(true);

ping02(ipAddress);

}

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ù)

}

}


運行結(jié)果:



心得與體會:本自作業(yè)詳細(xì)的去了解了ping的作用,但代碼的實現(xiàn)比較困難,對于gui的設(shè)計也用了很久時間,但是最后總算出來了,很勉強。

最后編輯于
?著作權(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)容

  • 完成一個ping的GUI界面的小程序: 新建窗口,設(shè)置窗口的大小。添加Label控件,設(shè)置字體和調(diào)整控件所處的位...
    一抹淺笑深白色_91fa閱讀 291評論 0 0
  • 題目要求: 1.編程作業(yè),要求用一種自己擅長的語言實現(xiàn)ip地址的獲取,及其詳細(xì)信息的顯示,要求用窗口顯示。 2.生...
    linlan96閱讀 372評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,626評論 18 399
  • “任何一種長期單一模式的生活,都是對自己犯罪!” 當(dāng)你看到這句話,不知你怎么理解,在我看來,說這句話的作者或許還很...
    長毛兔呀閱讀 1,856評論 0 0

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