infinite for loop problem

hello there

i'm trying to make a ping program that if the user presses a button

the program pings on a server continusely and print the response

so i used the infinite for loop but when i'm trying to exit the program

it doesn,t exit?how to fix that? here is my code

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.Container;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

publicclass ReachableTest2extends JFrame{

publicstaticvoid main(String args[]){

JButton btn=new JButton("Check The Ping");

JFrame frame =new JFrame("Ping Check");

frame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent ev){

System.exit(0);

}

});

ActionListener actionlistener=new ActionListener(){

publicvoid actionPerformed(ActionEvent evnt){

if(evnt.getActionCommand().equals("Check The Ping"))

{

try{

InetAddress address = InetAddress.getByName("212.122.224.10");

System.out.println("Name: " + address.getHostName());

System.out.println("Addr: " + address.getHostAddress());

for( ; ; )

{

System.out.println("Reach: " + address.isReachable(3000));

}

}

catch (UnknownHostException e){

System.err.println("Unable to lookup ");

}

catch (IOException e){

System.err.println("Unable to reach ");

}

}

}

};

frame.add(btn);

btn.addActionListener(actionlistener);

frame.setSize(250,150);

frame.setVisible(true);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

[3505 byte] By [microsmarta] at [2007-11-27 10:35:13]
# 1

Your actionPerformed method is called in AWT's event dispatcher thread.

Your infinite for loop blocks it from listening to any window event. You

should start a new thread for your pings which can be stopped from

the AWT event thread (or any other thread for that matter). Change your

infinite loop to something like this:

boolean stopped= false;

while (!stopped)

// do your pings here

And add a method to your thread which can be invoked from other threads:

public void setStopped() { this.stopped= true; }

kind regards,

Jos

JosAHa at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 2

for( ; ; )

{

System.out.println("Reach: " + address.isReachable(3000));

}

Any specific reason why you want to have an infinite loop here?

Anyway, use threads or make it so that the for loop will end.

Aww, i was too late. see reply 1

Message was edited by:

deAppel

deAppela at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 3

i've changed the loop code

but i don't know where to use this mehtod

public void setStopped() { this.stopped= true; }

i tried to use it in the windowlistener but it didnt work

frame.addWindowListener(new WindowAdapter() {

public void setStopped() { this.stopped= true; }

public void windowClosing(WindowEvent ev) {

System.exit(0);

}

});

where i should use it?

microsmarta at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 4

i also tried to define the method in the loop code to call it in the windowlistenere code but it also didn't work

microsmarta at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 5

As I wrote in my previous reply: you should run that loop in another thread.

As it is now your loop blocks the AWT thread from listening to events,

so you can't manipulate your application's components.

kind regards,

Jos

JosAHa at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 6

i can't get it because i'm a beginner

is there is a useful demo of something like that

microsmarta at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...
# 7

hi friend ,

i have made some changes to your code ,i hope this might help let me know if it works for you

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.Container;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class test extends JFrame {

public static void main(String args[]) {

JButton btn=new JButton("Check The Ping");

JFrame frame = new JFrame("Ping Check");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent ev) {

System.exit(0);

}

});

ActionListener actionlistener=new ActionListener() {

public void actionPerformed(ActionEvent evnt) {

if(evnt.getActionCommand().equals("Check The Ping"))

{

ping();

}

}

};

frame.add(btn);

btn.addActionListener(actionlistener);

frame.setSize(250,150);

frame.setVisible(true);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void ping()

{

Runnable r = new pinging();

Thread t = new Thread(r);

t.start();

}

}

class pinging implements Runnable {

public pinging()

{}

public void run()

{

try {

InetAddress address = InetAddress.getByName("212.122.224.10");

System.out.println("Name: " + address.getHostName());

System.out.println("Addr: " + address.getHostAddress());

for( ; ; )

{

System.out.println("Reach: " + address.isReachable(3000));

}

}

catch (UnknownHostException e) {

System.err.println("Unable to lookup ");

}

catch (IOException e) {

System.err.println("Unable to reach ");

}

}

}

Regards

coreJavaa at 2007-7-28 18:33:44 > top of Java-index,Java Essentials,New To Java...