Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Remote Desktop Administration

I am Working on a Java Remote desktop Administration . It works fine when i run the Server initiator main class seperately. but when i am calling that class from an action event of a button the frame just freezes and shows a black screen ..here is the code. Any Help ?

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class ServerInitiator {
    //Main server frame
    private JFrame frame = new JFrame();
    //JDesktopPane represents the main container that will contain all
    //connected clients' screens
    private JDesktopPane desktop = new JDesktopPane();

    public static void main(String args[]){
        String port = JOptionPane.showInputDialog("Please enter listening port");
       new ServerInitiator().initialize(Integer.parseInt(port));
    }

    public void initialize(int port){

        try {
            ServerSocket sc = new ServerSocket(port);
            //Show Server GUI
            drawGUI();
            //Listen to server port and accept clients connections
            while(true){
                Socket client = sc.accept();
                System.out.println("New client Connected to the server");
                //Per each client create a ClientHandler
                new ClientHandler(client,desktop);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /*
     * Draws the main server GUI
     */
    public void drawGUI(){
            frame.add(desktop,BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Show the frame in a maximized state
            frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
    }
}
like image 755
vinu_ser Avatar asked Aug 12 '26 11:08

vinu_ser


1 Answers

Your while loop is likely running from within the context of the Event Dispatching Thread, blocking it from processing any new events (including repaint events)

public void initialize(int port){

    try {
        ServerSocket sc = new ServerSocket(port);
        //Show Server GUI
        drawGUI();
        //Listen to server port and accept clients connections
        while(true){
            Socket client = sc.accept();
            System.out.println("New client Connected to the server");
            //Per each client create a ClientHandler
            new ClientHandler(client,desktop);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

See Concurrency in Swing for more details.

Instead, you should launch your server in a different Thread or use a SwingWorker if you want to be able to update the UI (simply and safely). See Worker Threads and SwingWorker for more details

like image 168
MadProgrammer Avatar answered Aug 14 '26 23:08

MadProgrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!