我在java中运行tcp服务器,这是使用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 :(得分:3)
注意: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);
}
});
希望这有帮助。
答案 1 :(得分: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");
}
}
}
}
}