基于服务器的SwingWorker不会停止 [英] Server based SwingWorker does not Stop

查看:137
本文介绍了基于服务器的SwingWorker不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔变量来控制服务器的执行(开始/停止):

I have a boolean variable to control the execution of the server (start/stop) :

private boolean ecoute = true;

private boolean ecoute=true;

这是我的班级:

 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+"\n");
            PrintWriter out=null;
            BufferedReader in=null;
            Socket socClient=null;
         while(ecoute){

                socClient = server.accept();  
                    log.append(str2+"\n");
                     in = new BufferedReader(
                                    new InputStreamReader(socClient.getInputStream())
                                   );
                   out = new PrintWriter(
                     new BufferedWriter(
                        new OutputStreamWriter(socClient.getOutputStream())), 
                     true);
                    String str = in.readLine();
                    log.append(str+"\n");

         }

            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+"\n");

       }
    };
   sw.execute();
  }

当我单击按钮将变量传递给false时,我的线程退出无限循环,没有任何反应:

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;

}

我替换了 ecoute = false; by sw.calcel(true); 也没什么新...

I replaced ecoute=false; by sw.calcel(true); also nothing new ...

任何建议?

推荐答案

您的ServerSocket导致循环无法终止。即使'ecoute'为false,server.accept()也将阻塞,直到满足以下两个条件之一:

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. 连接是发送到ServerSocket

  2. ServerSocket已关闭。

您需要执行以下操作之一两件事使得对server.accept()的调用将停止阻塞。请注意 - 如果您选择关闭ServerSocket,将抛出IOException。最好将调用包装在try块中的server.accept()中,而不是包装整个doInBackground()函数。

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.

这篇关于基于服务器的SwingWorker不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆