Question

I was trying to make a TCP GUI Server application for windows. But when i am trying to start the server it becomes unresponsive. I couldn't find out what is the problem here,somebody please give me a good solution for this bug...

code

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);
    }

}
Était-ce utile?

La solution

I couldn't find out what is the problem here

Well, you've got this code executing in your UI thread:

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

So when you start your UI, it is basically hanging the UI thread. You can't block the UI thread like this - it should be in a separate thread, marshalling over to the UI thread when necessary (to update the UI).

See Concurrency in Swing for more details of the threading rules of swing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top