Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

java - Server based SwingWorker does not Stop

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your ServerSocket is what's causing the loop to not terminate. Even though 'ecoute' is false, server.accept() will block until one of the following two conditions is met:

  1. A connection is made to the ServerSocket
  2. The ServerSocket is closed.

You need to do one of these two things so that the call to server.accept() will stop blocking. Mind you - if you choose to close the ServerSocket, an IOException will get thrown. It would probably be better to wrap the call to server.accept() in the try block, instead of wrapping the entire doInBackground() function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...