TCP服务器应用程序无响应

时间:2014-01-16 06:47:06

标签: java tcp tcpserver

我正在尝试为Windows创建一个TCP GUI Server应用程序。但是,当我试图启动服务器时,它变得没有响应。我不知道这里有什么问题,有人请给我一个很好的解决方案...

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;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class frame_example extends JFrame{


    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStreamReader inputStreamReader;
    private static BufferedReader bufferedReader;
    private static String message;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public frame_example(){
        //JFrame jf = new JFrame();
        JPanel jp = new JPanel();
        final JTextArea jt  = new JTextArea(10,20);
        JScrollPane scrolltxt = new JScrollPane(jt);
        scrolltxt.setBounds(0,0, 300, 200);
        //add(scrolltxt);
        JButton jb1 = new JButton("START");
        jb1.setActionCommand("Server Started..");
        JButton jb2 = new JButton("STOP");
        jb2.setActionCommand("Server has been Stoped..");

        jp.add(scrolltxt);
        jp.add(jb1);
        jp.add(jb2);
        getContentPane().add(jp);
        jb1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                   try {
                        serverSocket = new ServerSocket(4020);  //Server socket

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

        while (true) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            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);


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

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {

                    inputStreamReader.close();
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });


    }
    public static void main(String args[]){

        frame_example fe = new frame_example();

        fe.setSize(400, 200);
        fe.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:2)

  

我不知道这里有什么问题

嗯,你的代码在你的UI线程中执行

while (true) {
    try {
        Thread.sleep(20);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        clientSocket = serverSocket.accept(); 
        ...
    }
    ...
}

因此,当您启动UI时,它基本上是悬挂UI线程。你不能像这样阻止UI线程 - 它应该在一个单独的线程中,必要时编组到UI线程(更新UI)。

有关摆动线程规则的详细信息,请参阅Concurrency in Swing