I have this program that pings a specified host.
public class PingTool {
private String ip = "";
private int port;
private int times;
private String hostaddr = "";
String resultado;
public void setDireccion(String ip, int port, int times){
this.ip = ip;
this.port = port;
this.times = times;
}
//Metodo que ejecuta el programa.
public void exec(){
try {
hostaddr = InetAddress.getByName(ip).getHostAddress();.
} catch (UnknownHostException e) {
System.out.println("El host ingresado es incorrecto.");
System.exit(0);
}
resultado = "Haciendo ping a "+ip+" ("+hostaddr+") "+times+" veces en el puerto "+port+"...\n";
int total = 0;
long totalping = 0;
Socket s = null;
while(total < times) {
total++;
long start = System.currentTimeMillis();
try {
SocketAddress sockaddr = new InetSocketAddress(hostaddr, port);
s = new Socket();
s.connect(sockaddr, 1000);
} catch(SocketTimeoutException e) {
resultado = "Respuesta desde ["+hostaddr+"]: Tiempo de espera agotado.";
continue;
} catch(UnknownHostException e) {
} catch(IOException e) {
}
long end = System.currentTimeMillis();
totalping += (end-start);
long totaltime = (end-start);
long avg = (long)(totalping/total);
if(totaltime <= 0){
totaltime++;
resultado = "Respuesta desde "+hostaddr+" Tiempo: "+ "<" + totaltime + "ms" +" Average: "+avg;
}else{
resultado = "Respuesta desde "+hostaddr+" Tiempo: "+ totaltime + "ms" +" Average: "+avg;
}
}
long avg = (long)(totalping/total);
resultado = "\nResultado de ping para: " + hostaddr;
resultado = "\nAverage request - " + avg;
}
}
And I have another class in which I have the graphical interface where the result is a textarea.
PingTool pingTool = new PingTool();
String ip = campoIP.getText();
int port = Integer.parseInt(campoPort.getText());
int times = Integer.parseInt(campoTimes.getText());
pingTool.setDireccion(ip, port, times);
pingTool.exec();
resultadoPing.append(result + "\n");
What I can not do is print the result textarea every cycle turn, for example if I want to ping 10 times in the textarea should appear those 10 results. But the only thing I get is to print the last value that is assigned to the variable that contains the result.