【问题标题】:Javafx get Ping ResultJavafx 获取 Ping 结果
【发布时间】:2014-07-28 11:27:17
【问题描述】:

我编写了一个类来 ping 我提供的 IP 地址,但它不会返回任何内容。 我尝试添加一些标记以查看哪里出错了,但即使这样也没有用... 我有一个 gui 界面,我使用一个标签来写出我的数据(以前使用字符串的相同格式),这里是代码。我想要或不想要某些行,因此“相关”整数,您可以忽略它。这应该在 ubuntu 13.10 上运行。

public static ArrayList<String> PingIpAddr(String string) throws IOException{
        String s = new String();
        int relevant =0;
        ArrayList<String> List = new ArrayList<String>();
        List.add("it happens \n");
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(new ProcessBuilder(string).start().getInputStream()));
        while ((s = stdInput.readLine()) != null){
             List.add("does this happen? \n");
            relevant++;
            if( (relevant == 2) || (relevant == 3) || (relevant == 4) || (relevant == 5) || (relevant == 6) || (relevant == 9) ){List.add(s + "\n");
            List.add("or this? \n");}}  //end of while
        List.add("This must happen! \n");
        return List;}   //end of Ping

如果这行得通,这里就是实施它的地方:

    String test;
    test = PingIp.testPingIpAddr("ping -c 5 4.2.2.2").toString();
    TeltonikaPing.setWrapText(true);
    TeltonikaPing.setText(test);

奇怪的是它没有返回一行。也许我只是错过了一些非常基本的东西?:/

【问题讨论】:

    标签: java user-interface javafx ping


    【解决方案1】:
    1. 主要问题是由于在大多数情况下 ping 有延迟,请尝试使用 stdInput.ready()。

    2. 我可能会将其传递给 ProcessBuilder:new ProcessBuilder("myCommand", "myArg1", "myArg2");将命令 ping 从其参数中分离出来。

    如 - http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

    我希望这会有所帮助(:

    编辑——(在下面有效)

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Pinger
    {
        public static List<String> PingIpAddr(String ip) throws IOException
        {
            ProcessBuilder pb = new ProcessBuilder("ping", ip);
            //ProcessBuilder pb = new ProcessBuilder("ping", "-c 5", ip);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(pb.start().getInputStream()));        
    
            while (!stdInput.ready())
            {
                // custom timeout handling
            }
    
            String line;
            ArrayList<String> output = new ArrayList<>();
    
            while ((line = stdInput.readLine()) != null)
            {
                output.add(line);
            }
    
            return output;
        }
    
        public static void main(String[] args) throws IOException
        {
            List<String> lines = Pinger.PingIpAddr("127.0.0.1");
    
            for (String line : lines)
            {
                System.out.println(line);
            }
        }
    }
    

    【讨论】:

    • 它确实 ^^,一旦我得到这个工作,我会将此标记为答案。可悲的是,我还没有代表支持它:D
    • 我试过了,问题依旧,我修改成这样:
    • public static ArrayList testPingIpAddr(String string) throws IOException{ String s = new String(); ArrayList List = new ArrayList(); List.add("它发生了\n"); BufferedReader stdInput = new BufferedReader(new InputStreamReader(new ProcessBuilder("ping -c 5 " + string ).start().getInputStream()));
    • while ((s = stdInput.readLine()) != null){ List.add("这会发生吗?\n"); if(stdInput.ready() == false){ List.add("or this?\n"); try {Thread.sleep(1500);} catch(InterruptedException ex) {Thread.currentThread().interrupt();};} // 结束 if List.add(s + "\n"); List.add("这必须发生!\n");} //while 结束 List.add("ping -c 5 " + string + "\n"); return List;} //Ping 结束
    • 除此之外,我无法制作 oracle 文档的正面或反面.. 抱歉 :(
    【解决方案2】:

    我找到了另一个解决方案,可以让我输入我希望 Ping 保持多长时间。

    How to run PING command and get ping host summary?

    如果其他人需要将其合并到 GUI 中:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多