Question

I am trying to create a remote-desktop monitor application using java where a server can see the screenshot of the client and also can monitor the number of processes running on the client. Here is the screenshot of the user interface

enter image description here

But now I have a bug which I can't fix. I have debugged it several times but I couldn't find a way to track the bug. Here are the problems which I am facing right now:=

1) When I press the "GET PROCESS LIST" button nothing happens. The process list is fetched to the textarea when the capture button is pressed i.e First I have to press the "GET PROCESS LIST" then I have to press the capture button for the list of processes to show up.

2) When I press the capture button it only captures the screenshot of the client once. After that when I press the capture button it doesn't do anything. Same is with the Get Process Button. They only get executed once.

I have tried debugging both the client and the server but failed to track the bug.

Here is my code for client and server. The code for server is big but I have to provide the whole code as because I don't know where the problem actually lies.

This is the client

public class ScreenClient {

    private static BufferedImage screen;


    public static void main(String[] args) {
        try {
            Socket server = new Socket("localhost", 5494);

            while (true) {
                BufferedReader bf = new BufferedReader(new InputStreamReader(server.getInputStream()));
                String s;
                s = bf.readLine();

                System.out.println(s);
                if (s.contains("execute")) {

                    ImageThread im=new ImageThread();
                    im.start();

                }
                if (s.contains("getProcessList")) {

                    ProcessThread pt=new ProcessThread();
                    pt.start();

                }


            }
        } catch (Exception e) {
            System.err.println("Disconnected From server ->" + e.getMessage());
        }
    }

    public static class ImageThread extends Thread {

        @Override
        public void run() {

            try {
                Socket server = new Socket("localhost", 9999);//connect to server at port 9999 for screenshots
                Robot robot = new Robot();
                Rectangle size = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

                screen = robot.createScreenCapture(size);

                int[] rgbData = new int[(int) (size.getWidth() * size.getHeight())];

                screen.getRGB(0, 0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());

                OutputStream baseOut = server.getOutputStream();

                ObjectOutputStream out = new ObjectOutputStream(baseOut);

                out.writeObject(size);

                //for (int x = 0; x < rgbData.length; x++) {

                out.writeObject(rgbData);

                // }
                out.flush();
                server.close();


                //added new
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Disconnected From server ->" + e.getMessage());
            }
        }
    }

    public static class ProcessThread extends Thread {



        @Override
            public void run() {

            System.out.println("\n\n********");
                StringBuilder builder = new StringBuilder("");
                Socket server;
                PrintWriter ps;
                String query = "tasklist";
                try {
                    server = new Socket("localhost", 9898);//connect to server at port 9898 for process list
                    Runtime runtime = Runtime.getRuntime();
                    InputStream input = runtime.exec(query).getInputStream();
                    BufferedInputStream buffer = new BufferedInputStream(input);
                    BufferedReader commandResult = new BufferedReader(new InputStreamReader(buffer));
                    String line = "";

                    while ((line = commandResult.readLine()) != null) {
                        builder.append(line + "\n");
                    }
                    //byte[] responseClient=s.getBytes();
                    System.out.println("Obj ::" + server);
                    ps = new PrintWriter(server.getOutputStream());
                    ps.write(builder.toString());
                    System.out.println(builder.toString());
                    ps.flush();
                    server.close();



                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

Here is the Server's code:=

public class ScreenServer extends JFrame implements ActionListener {


    String uname;
    BufferedReader br;
    static JTextArea taMessages, taUserList, taProcessList;
    JTextField tfInput;
    static Socket client, client1,client2;//
    JButton btnCapture, btnExit, btnGetUsers, btnGetProcess;
    static HashMap<String, Socket> users = new HashMap<>();
    static ArrayList<Socket> usersSockets = new ArrayList();
    PrintWriter os;
    PrintWriter os2;

    public static void main(String[] args) throws Exception {


        ScreenServer screenServer = new ScreenServer();
        screenServer.buildInterface();
        ServerSocket server = new ServerSocket(5494);
         ServerSocket s1 = new ServerSocket(9898);
         ServerSocket s2=new ServerSocket(9999);
        while (true) {



            ScreenServer.client = server.accept();

            users.put(client.getInetAddress().getHostName(), client);
            usersSockets.add(client);       //not needed
            InetAddress inet1 = client.getInetAddress();

            broadcast(inet1.getHostName(), " Has connected!");

            client1=s2.accept();//accept connection on port 9999
           ScreenshotThread st = new ScreenshotThread(client1);
            st.start();
            client2=s1.accept();//accept connection on port 9898
            ProcessListClient clientProcessList = new ProcessListClient(client2);
            clientProcessList.start();


        }


    }

    public void buildInterface() {
        btnCapture = new JButton("Capture Screen");
        btnExit = new JButton("Exit");
        btnGetUsers = new JButton("Get Users");
        btnGetProcess = new JButton("Get Process List");


        //chat area
        taMessages = new JTextArea();
        taMessages.setRows(10);
        taMessages.setColumns(50);
        taMessages.setEditable(false);
        taProcessList = new JTextArea();
        taProcessList.setRows(10);
        taProcessList.setColumns(50);
        taProcessList.setEditable(false);
        //online users list
        taUserList = new JTextArea();
        taUserList.setRows(10);
        taUserList.setColumns(10);
        taUserList.setEditable(false);
        //top panel (chat area and online users list
        JScrollPane chatPanel = new JScrollPane(taMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JScrollPane onlineUsersPanel = new JScrollPane(taUserList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JScrollPane processPanel = new JScrollPane(taProcessList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JPanel tp = new JPanel(new FlowLayout());
        tp.add(chatPanel);
        tp.add(onlineUsersPanel);
        tp.add(processPanel);
        add(tp, "Center");
        //user input field
        tfInput = new JTextField(50);
        //buttom panel (input field, send and exit)
        JPanel bp = new JPanel(new FlowLayout());
        bp.add(tfInput);
        bp.add(btnCapture);
        bp.add(btnExit);
        bp.add(btnGetUsers);
        bp.add(btnGetProcess);

        add(bp, "South");
        btnCapture.addActionListener(this);
        tfInput.addActionListener(this);//allow user to press Enter key in order to send message
        btnExit.addActionListener(this);
        btnGetUsers.addActionListener(this);
        btnGetProcess.addActionListener(this);
        setSize(500, 300);
        setVisible(true);
        pack();
    }

    @Override
    public void actionPerformed(ActionEvent evt) {

        if (evt.getSource() == btnExit) {


            System.exit(0);
        } else if (tfInput.getText().contains("!getusers")) {
            taUserList.setText(" ");
            this.getUsersOnline();

        } else if (evt.getSource() == btnCapture) {
            MessagesThread mt = new MessagesThread(client);
            mt.start();


        }

        if (evt.getSource() == btnGetUsers) {
            taUserList.setText("");
             System.out.println("ERROR--> No Client Connection Found");

        }
        if (evt.getSource() == btnGetProcess) {

            GetProcessThread gt = new GetProcessThread(client);
            gt.start();



        }
    }

    private void getUsersOnline() {
        Iterator it = users.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            putUserOnline((String) pairs.getKey());
        }

    }

    public static void broadcast(String user, String message) {

        putMessage(user, message);

    }

    public static void putMessage(String uname, String msg) {
        taMessages.append(uname + ": " + msg + "\n");
    }

    public static void putUserOnline(String name) {
        taUserList.setText(name + ": " + "\n");
    }

    public class MessagesThread extends Thread {
       Socket client;

        public MessagesThread(Socket client) {
            this.client = client;
        }

        @Override
        public void run() {
            String ClientName = tfInput.getText();
            try {
                String response = "execute";
                Socket clientValue = users.get(ClientName);

                os = new PrintWriter(clientValue.getOutputStream(), true);
                os.println(response);


            } catch (IOException ex) {
                Logger.getLogger(ScreenServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    //here

    public class GetProcessThread extends Thread {

        Socket client;

        public GetProcessThread(Socket client) {
            this.client = client;
        }

        @Override
        public void run() {
            String ClientName = tfInput.getText();
            try {
                String response = "getProcessList";
                Socket clientValue = users.get(ClientName);

                os2 = new PrintWriter(clientValue.getOutputStream(), true);
                os2.println(response);


            } catch (IOException ex) {
                Logger.getLogger(ScreenServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    //here
    static class ScreenshotThread extends Thread {

        BufferedImage screen;
        Socket client;
        public ScreenshotThread(Socket client) {
          this.client=client;


        }

        @Override
        public void run() {

                try {
                    Robot robot = new Robot();
                    ObjectInputStream in = new ObjectInputStream(client.getInputStream());

                    Rectangle size = (Rectangle) in.readObject();

                    int[] rgbData = new int[(int) (size.getWidth() * size.getHeight())];

                  /*  for (int x = 0; x < rgbData.length; x++) {

                        rgbData[x] = in.readInt();

                    }*/
                    rgbData=(int[])in.readObject();





                    screen = new BufferedImage((int) size.getWidth(), (int) size.getHeight(), BufferedImage.TYPE_INT_ARGB);

                    screen.setRGB(0, 0, (int) size.getWidth(), (int) size.getHeight(), rgbData, 0, (int) size.getWidth());
                    InetAddress clientAddress = client.getInetAddress();
                    String clientName = clientAddress.getHostName();
                    String t = (new Date()).toString();
                    ImageIO.write(screen, "png", new File(clientName + ".png"));
                } catch (Exception ioe) {
                    ioe.printStackTrace();
                }

        }
    }

    static class ProcessListClient extends Thread {

        Socket client;
        BufferedReader is;

        public ProcessListClient(Socket s1) {
         this.client=s1;
            }

        @Override
        public void run() {
            StringBuilder stringBuilder = new StringBuilder("");
            try {
                this.is = new BufferedReader(new InputStreamReader(client.getInputStream()));
                String line = "";

                while ((line = is.readLine()) != null) {
                    stringBuilder.append(line + "\n");
                }
                System.out.println(stringBuilder);
                taProcessList.setText(stringBuilder.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}

I think I have stated all the issues and provided relevant code.I would really appreciate any help. :)

Was it helpful?

Solution

ServerSocket server = new ServerSocket(5494);
ServerSocket s1 = new ServerSocket(9898);
ServerSocket s2=new ServerSocket(9999);
while (true) {

            ScreenServer.client = server.accept();
            client1=s2.accept();//accept connection on port 9999
            client2=s1.accept();//accept connection on port 9898

Here server.accept() will not go to next line unless server gets a client, similarly s2.accept() will not go to next line unless s2 gets a client. So all three ServerSockets should should accept() in separate threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top