0

我得到了一个稍作修改的示例 ping 程序......

    String ip = "192.168.1.1 -t";
    String pingResult = "";      
    String pingCmd = "ping " + ip;

    try{

        Runtime r = Runtime.getRuntime();
        Process p = r.exec(pingCmd);

        Pattern pattern = Pattern.compile("time=(\\d+)ms");
        Matcher m = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            m = pattern.matcher(inputLine);
            if (m.find()) {
                System.out.println(m.group(1));
            }
        }
        in.close();

    }
    catch (IOException e) {
        System.out.println(e);
    }

我将实现一个包含开始和退出按钮的 GUI,开始按钮可以工作,但退出按钮不会。当我单击退出按钮时,程序不显示 rtt 时间输出。但是程序实际上并没有退出/停止。我如何完全关闭它,我要添加类似Runtime.getRuntime().addShutdownHook.

需要一些提示和指南,在此先感谢...

4

2 回答 2

0

您可以使用 ping 的-n参数并在 n 次 ping 后重新启动,或者在用户需要停止时停止。

Process还有一个OutputStream。当您想停止 ping 时,您可以尝试发送 Ctrl-C。

于 2013-03-04T11:11:17.420 回答
0
boolean isStoped=false;

while ((inputLine = in.readLine()) != null && (!isStoped)) {
            m = pattern.matcher(inputLine);
            if (m.find()) {
                System.out.println(m.group(1));
            }
        }

当您单击退出按钮时拨打isStoped=true和拨打电话。p.destroy()希望这会帮助你。

于 2013-03-23T19:44:11.727 回答