【问题标题】:send a ping to multiple ip addresses at the same time同时向多个 IP 地址发送 ping
【发布时间】:2014-05-07 22:22:33
【问题描述】:

我做了一个代码,希望向多个 IP 地址发送 ping,并从每个 ping 请求中获取时间值,然后将结果打印到文本文件中的矩阵中......无论如何我的问题是在我完成我的代码后,我发现我应该同时将 ping 发送到这些地址,或者在我的代码中我确实将它连续发送到这些地址。我真的希望有人能帮助我...... 代码:

   import java.awt.Color;
   import java.awt.Container;
   import java.awt.FlowLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.io.BufferedReader;
   import java.io.FileNotFoundException;
   import java.io.IOException;
    import java.io.InputStreamReader;
   import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Formatter;
   import java.util.List;

   import javax.swing.JButton;
  import javax.swing.JFrame;
    import javax.swing.JLabel;
  import javax.swing.JTextField;
   import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;

  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
   import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;


 public class Pinggg extends JFrame{
    private boolean stop = false;  // start or stop the ping



public Pinggg(){
   Container cp = this.getContentPane();
   cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

   JButton btnStart=new JButton("demarrer le test");   
   cp.add(btnStart);
   btnStart.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent evt) {

        stop = false;
        try {
            final Formatter x = new Formatter("C:/Users/VAIO/workspace/tcc/gastoon/kkk.txt");
            PrintWriter writer;
            writer = new PrintWriter("C:Users/VAIO/workspace/tcc/gastoon/kkk.txt");

             for (int m = 0; m < 10; m++) {
                 if (stop) break;
                     // check if STOP button has been pushed,

                                    //  which changes the stop flag to true



               DocumentBuilderFactory BuilderFactory=DocumentBuilderFactory.newInstance(); 


                 DocumentBuilder db=BuilderFactory.newDocumentBuilder();
                 Document doc=db.parse("C:/Users/VAIO/workspace/tcc/gastoon/adresStore.xml");
                 doc.normalize();


                 NodeList rootNodes=doc.getElementsByTagName("connection");
                 Node rootNode=rootNodes.item(0);
                 Element rootElement=(Element) rootNode;

                 NodeList l=rootElement.getElementsByTagName("users");
                 Node users=l.item(0);
                 Element element=(Element) users;

                 NodeList nl=element.getElementsByTagName("user");
                 List<String> allIps = new ArrayList<String>();

                  for(int i=0;i<nl.getLength();i++){

                   Node user=nl.item(i);
                   Element ele=(Element) user;
                   String adrss=ele.getElementsByTagName("ipAdress").item(0).getTextContent();//+" -n 1";
                   //System.out.println(adrss);
                   allIps.add(i, adrss);
                   //writer.print(allIps.get(i)+" ");
                   //  System.out.println(adrss);
                   //System.out.println(i);

                   //   writer.format("%s  ",i);
                   //writer.println( adrss);
                  }


                //  for(String n : allIps)

               //{
                //    writer.print(allIps+" ");   
               //}
                  writer.println("\n");






                    for(int j=0;j<allIps.size();j++)
                     {
                        //writer.print(allIps.get(i)+" ");


                         String pingCmd = "ping " +allIps.get(j) +" -n 1";
                         String pingResult = "";
                         String str="";

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

                           BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
                           String inputLine;
                             while ((inputLine = in.readLine()) != null) 
                                    {

                               // writer.println(inputLine);

                                //System.out.println(inputLine);
                                  pingResult += inputLine;
                                    }

                                 String[] lines = pingResult.split("\n");
                                 List<String> bb = new ArrayList<String>();

                                    for (int k=0;k<lines.length;k++) {



                                     String line=lines[k];

                                      if ((line.contains("temps=") && (line.contains(allIps.get(j))))){
                                       // Find the index of "time="
                                       int index = line.indexOf("temps=");
                                       String time = line.substring(index + "temps=".length(),line.indexOf("ms"));
                                       //bb.add(time);
                                     //  writer.print(allIps.get(j)+" ");
                                       writer.print(time);

                                       //System.out.println(allIps.get(j)+" ");

                                       System.out.println(time);
                                         }
                                      else {
                                          writer.print("NON"+" ");
                                      }

                                       }
                                      int[]tab=new int[allIps.size()];
                                        for(int d=0;d<tab.length;d++ ){

                                    }


                                    }





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


                }}

          writer.flush(); 
             }//}


  catch (SAXException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} 
   catch (IOException e1)
{
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

    catch (ParserConfigurationException e1)
  {
// TODO Auto-generated catch block
e1.printStackTrace();
  } 


              }    

        }



   );






      JButton btnStop = new JButton("Analyser le test ");
      cp.add(btnStop);
      btnStop.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent evt) {
            stop = false;  // set the stop flag
         }
      }); 






      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      cp.setBackground(Color.black);
      setTitle("PING");
      setSize(300, 120);
      setVisible(true);

      }     

       }        

【问题讨论】:

  • @yoonix,投票迁移问题,我们将把它转移过来。
  • 这种格式太可怕了……
  • 您的问题是什么?这只是一个 200 多行代码转储。

标签: java ping xml text


【解决方案1】:

这取决于您所说的“我应该同时将 ping 发送到这些地址 [原文如此]”。

1) 创建一个 threadppol 并通过它发送您的请求。这将批量处理您的 ping 请求(到线程池的大小)。然而,这在技术上不是“同时”。

2) Ping 广播地址。这是否有效(或者更确切地说是您认为的有效方式)取决于您的网络设置。它不能在 Internet 上运行,但它可以在公司 LAN 上运行。因此,这取决于您尝试 ping 的 IP 地址的性质。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多