题目:写一个实现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 ; //超时应该在3钞以上
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是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(); // 将要执行的ping命令,此命令是windows格式的命令
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 执行命令并获取输出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream(),"UTF-8")); // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace(); // 出现异常则返回假
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
private static int getCheckResult(String line) { // System.out.println("控制台输出的结果为:"+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出现的位置,str即上面的字符串
n = line.indexOf(":");
line = line.substring(n + 2); // 获得 :后面的数据;
break;
}
}
ps.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return line; // 返回数据
}
}