題目:寫一個(gè)實(shí)現(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 ;? //超時(shí)應(yīng)該在3鈔以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);? ? // 當(dāng)返回值是true時(shí)昭雌,說明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"));? // 逐行檢查輸出,計(jì)算類似出現(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ù)=測(cè)試次數(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("控制臺(tái)輸出的結(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("主機(jī)名? . . . . . . . . . . . . . :");
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("時(shí)間:" + 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; // 返回?cái)?shù)據(jù)
}
}