I have a boolean variable to control the execution of the server (start/stop) :
private boolean ecoute=true;
here is my class:
sw=new SwingWorker<String,Void> (){
protected String doInBackground() throws Exception {
try {
server = new ServerSocket(Integer.parseInt(port.getText()));
String str1="waiting for connexion..";
String str2="Connexion ok";
log.append(str1+"
");
PrintWriter out=null;
BufferedReader in=null;
Socket socClient=null;
while(ecoute){
socClient = server.accept();
log.append(str2+"
");
in = new BufferedReader(
new InputStreamReader(socClient.getInputStream())
);
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socClient.getOutputStream())),
true);
String str = in.readLine();
log.append(str+"
");
}
in.close();
out.close();
socClient.close();
return "***End Reception***";
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
return "***Error Reception***";
}
}
protected void done(){
String m="";
try {
m=get();
} catch (InterruptedException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
log.append(m+"
");
}
};
sw.execute();
}
when I click the button to pass the variable to false for my thread get out of the infinite loop,nothing happens :
private void arretReceptionActionPerformed(java.awt.event.ActionEvent evt) {
ecoute=false;
}
I replaced ecoute=false;
by sw.calcel(true);
also nothing new ...
any suggestion ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…