Print cycle while in a TextArea that is in another Java class

0

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.

    
asked by CodeBlack 14.08.2017 в 05:03
source

2 answers

1

It is not well understood in which part you are sending things to your view.

However, at no time are you putting together a chain with all the results.

Look for example:

resultado = "Respuesta desde "+hostaddr+" Tiempo: "+ totaltime + "ms" +" Average: "+avg;

these lines, always overwrite the result value, they are not adding it to those that were before, therefore, there is always the last value.

you should use something of the style:

resultado += "Respuesta desde "+hostaddr+" Tiempo: "+ totaltime + "ms" +" Average: "+avg;

This last depends on the new chain, on the chain that was before ... and that for all the places where you add data to the result.

    
answered by 14.08.2017 / 05:33
source
0

What I do is concatenate all the answers. Concatenate what you have inside the while, but at the end of each interaction add a ";", in the view you only do TextArea.setText (Respuesta.replace (";", " \ n ")).

    
answered by 14.08.2017 в 06:05