我做了一个代码希望向多个ip地址发送ping并从每个ping请求获取时间值然后打印将结果写入文本文件中的矩阵....无论如何我的问题是在我完成我的代码后,我发现我应该在同一时间或在我的代码中将ping发送到这些地址我连续发送到adresses.i真的我希望如果有人帮助我... 代码:
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);
}
}
答案 0 :(得分:0)
这取决于你的意思“我应该同时将ping发送到这些地址[原文如此]。”
1)创建一个threadppol并通过它发送您的请求。这将批量ping请求(到线程池的大小)。然而,这在技术上并非“同时”。
2)Ping广播地址。这是否有效(或者更确切地说,您认为它的工作方式)取决于您的网络设置。它不能在Internet上运行,但它可以在公司LAN上运行。因此,这取决于您尝试ping的IP地址的性质。