2014-02-19 54 views
2

我在java中运行一个使用SwingWorker类的tcp服务器。第一次它会开始成功,但是当我停下来并开始这个过程时,它将无法工作。我找不到什么漏洞,请帮我解决这个我无法停止Java SwingWorker进程

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class SwingWorkerDemo extends JFrame { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private static ServerSocket serverSocket; 
     private static Socket clientSocket; 
     private static InputStreamReader inputStreamReader; 
     private static BufferedReader bufferedReader; 
     private static String message; 

    public SwingWorkerDemo() { 
     initialize(); 
    } 

    private void initialize() { 
     this.setLayout(new FlowLayout()); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JButton startButton = new JButton("Start"); 
     final JButton stopButton = new JButton("Stop"); 
     final LongRunProcess process = new LongRunProcess(); 



     startButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       try { 
        process.execute(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       startButton.setEnabled(false); 
       stopButton.setEnabled(true); 
      } 
     }); 

     stopButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       // JOptionPane.showMessageDialog(null, "Hello There"); 

process.cancel(true); 
startButton.setEnabled(true); 
stopButton.setEnabled(false); 
      } 
     }); 


     this.getContentPane().add(startButton); 
     this.getContentPane().add(stopButton); 

     this.pack(); 
     this.setSize(new Dimension(300, 80)); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new SwingWorkerDemo().setVisible(true); 
      } 
     }); 
    } 

    class LongRunProcess extends SwingWorker { 
     /** 
     * @throws Exception 
     */ 
     protected Object doInBackground() throws Exception { 
      try { 
       serverSocket = new ServerSocket(4545); //Server socket 

      } catch (IOException e) { 
       System.out.println("Could not listen on port: 4545"); 
      } 

      System.out.println("Server started. Listening to the port 4545"); 

      while (true) { 
       try { 

        clientSocket = serverSocket.accept(); //accept the client connection 
        inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); 
        bufferedReader = new BufferedReader(inputStreamReader); //get the client message 
        message = bufferedReader.readLine(); 
    if(message.equals("shutdown")){ 

    Runtime runtime = Runtime.getRuntime(); 
    Process proc = runtime.exec("shutdown -s -t 00"); 
    System.exit(0); 

    } 
    else if(message.equals("restart")){ 
    Runtime runtime1 = Runtime.getRuntime(); 
    Process proc2 = runtime1.exec("shutdown -r -t 00"); 
    System.exit(0); 


    } 

        System.out.println(message); 
        inputStreamReader.close(); 
        clientSocket.close(); 

       } catch (IOException ex) { 
        System.out.println("Problem in message reading"); 
       } 
      } 

    } 
    } 
} 
+0

我看不到任何关闭服务器套接字尝试的跟踪。 –

+0

我如何阻止它从UI线程? –

+0

您是否已经尝试调用'.close()'? –

回答

1

您可以使用它。在此我也做了一些更改

public class Test extends JFrame { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private static ServerSocket serverSocket; 
    private static Socket clientSocket; 
    private static InputStreamReader inputStreamReader; 
    private static BufferedReader bufferedReader; 
    private static String message; 

    public Test() { 
     initialize(); 
    } 

    LongRunProcess process; 

    private void initialize() { 
     this.setLayout(new FlowLayout()); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JButton startButton = new JButton("Start"); 
     final JButton stopButton = new JButton("Stop"); 

     startButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       try { 
        process = new LongRunProcess(); 
        process.start(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       startButton.setEnabled(false); 
       stopButton.setEnabled(true); 
      } 
     }); 

     stopButton.addActionListener(new ActionListener() { 
      @SuppressWarnings("deprecation") 
      public void actionPerformed(ActionEvent e) { 
       // JOptionPane.showMessageDialog(null, "Hello There"); 

       process.closeServer(); 

       startButton.setEnabled(true); 
       stopButton.setEnabled(false); 
       process.stop(); 
      } 
     }); 

     this.getContentPane().add(startButton); 
     this.getContentPane().add(stopButton); 

     this.pack(); 
     this.setSize(new Dimension(300, 80)); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new Test().setVisible(true); 
      } 
     }); 
    } 

    class LongRunProcess extends Thread { 
     /** 
     * @throws Exception 
     */ 
     public void closeServer() { 
      try { 
       serverSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      System.out.println("Closed Now"); 
     } 

     public void run() { 
      try { 
       serverSocket = new ServerSocket(4545); // Server socket 

      } catch (IOException e) { 
       System.out.println("Could not listen on port: 4545"); 
      } 

      System.out.println("Server started. Listening to the port 4545"); 

      while (!(serverSocket.isClosed())) { 
       try { 

        clientSocket = serverSocket.accept(); // accept the client 
                  // connection 
        inputStreamReader = new InputStreamReader(
          clientSocket.getInputStream()); 
        bufferedReader = new BufferedReader(inputStreamReader); // get 
                      // the 
                      // client 
                      // message 
        message = bufferedReader.readLine(); 
        if (message.equals("shutdown")) { 

         Runtime runtime = Runtime.getRuntime(); 
         Process proc = runtime.exec("shutdown -s -t 00"); 
         System.exit(0); 

        } else if (message.equals("restart")) { 
         Runtime runtime1 = Runtime.getRuntime(); 
         Process proc2 = runtime1.exec("shutdown -r -t 00"); 
         System.exit(0); 

        } 

        System.out.println(message); 
        inputStreamReader.close(); 
        clientSocket.close(); 

       } catch (IOException ex) { 
        System.out.println("Problem in message reading"); 
       } 
      } 
     } 
    } 
} 
+0

谢谢你现在的工作.... –

3

按的Javadoc的SwingWorker#execute()

注:SwingWorker的被设计为只执行一次。多次执行一个 SwingWorker不会导致两次调用doInBackground方法 。

因此您看到的行为。所以为了达到你的目标。每次按下开始按钮时都需要创建LongRunProcess的新实例。像这样的东西。

在申报一流水平

LongRunProcess process = null; 

修改的动作监听

startButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      try { 
       process = new LongRunProcess(); 
       process.execute(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      startButton.setEnabled(false); 
      stopButton.setEnabled(true); 
     } 
    }); 

希望这有助于。

+0

我不是想以串行方式执行它,我只是想启动 - 停止程序,所以通常只有一个swingworker正在运行 –

+0

是的,但是你只是在intialize方法中创建一次LongRunProcess实例,所以你试图重新启动它,一次又一次使用相同的实例。您必须在按下开始按钮后创建一个LongRunProcess的新实例。 – Sanjeev

+0

但我如何停止当前正在运行的过程还关闭打开的端口,否则它会抛出异常端口正在使用 –