'Server Pinger Multithreading Issues
I am making a GUI server pinger application, and have multithreaded each port on a list so that each port is check by a separate thread. The problem I am having is that sometimes the port is check twice (according to the output). Here is my code: Class PingThread:
package serverpinger;
import java.io.IOException;
import java.net.InetAddress;
import java.rmi.UnknownHostException;
public class PingThread extends Thread{
TextFieldOutput output;
private String IPAddress;
private int portNo;
public PingThread(String IPAdress, int portNo,TextFieldOutput output){
this.IPAddress=IPAdress;
this.portNo=portNo;
this.output=output;
}
@Override
public void run(){
String result="";
try{
InetAddress address=InetAddress.getByName(IPAddress);
if(address.isReachable(portNo)){
result+="Reachable at: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
else{
result+="Unreachable at: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
}
catch(UnknownHostException uhe){
result+="UnknownHostException for: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
catch(IOException ioe){
result+="IOException for: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
output.setText(output.getText()+result);
}
}
Class ButtonRun:
package serverpinger;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
public class ButtonRun extends JButton{
TextFieldInput input;
TextFieldOutput output;
public ButtonRun(TextFieldInput input,TextFieldOutput output){
super("Run");
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Scanner scanner=new Scanner(input.getText());
input.selectAll();
input.replaceSelection("");
String IPAddress=scanner.next();
while(scanner.hasNext()){
String port=scanner.next();
if(port.contains("-")){
for(int portNo=Integer.parseInt(port.substring(0,4));
portNo<=Integer.parseInt(port.substring(5,9));
portNo++){
PingThread ping=new PingThread(IPAddress,portNo,output);
ping.start();
}
}
else{
PingThread ping=new PingThread(IPAddress,Integer.parseInt(port),output);
ping.start();
}
}
}
});
}
}
The rest of the classes are just subclasses of various JComponents, tailored to fit the application and make it look pretty.
Also, on occasion, the output will yield a line with random numbers (i.e. 7, 677) or a line that has the first two characters taken off (i.e. achable at IPAddress: example Port: example).
I don't know the source of any of the three glitches except that I am likely doing something wrong with the multithreading.
EDIT: I tried putting it on the Event Dispatch Thread and it still does not work, reproducing the same glitches. Here is the PingThread class now:
package serverpinger;
import java.io.IOException;
import java.net.InetAddress;
import java.rmi.UnknownHostException;
import javax.swing.SwingWorker;
public class PingThread extends SwingWorker{
TextFieldOutput output;
private String IPAddress;
private int portNo;
public PingThread(String IPAdress, int portNo,TextFieldOutput output){
this.IPAddress=IPAdress;
this.portNo=portNo;
this.output=output;
}
@Override
public PingThread doInBackground(){
String result="";
try{
InetAddress address=InetAddress.getByName(IPAddress);
if(address.isReachable(portNo)){
result+="Reachable at: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
else{
result+="Unreachable at: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
}
catch(UnknownHostException uhe){
result+="UnknownHostException for: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
catch(IOException ioe){
result+="IOException for: IPAddress: "+IPAddress+" Port: "+portNo+"\n";
}
output.setText(output.getText()+result);
return this;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
