2 years ago

#30248

test-img

Oside

What does Commands.____.getAbbrev()) do in this code?

I am pretty new to the Robot class so I need some help understanding a piece of code. The point of the project is to create a remote desktop and so while reviewing some code on the internet I couldn't figure out a specific part of the code was doing.

Server side:

import java.awt.Robot;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
/* Used to recieve server commands then execute them at the client side*/

class ReceiveEvents extends Thread{
            Socket socket= null;
            Robot robot = null;
            boolean continueLoop = true;

        public ReceiveEvents(Socket socket, Robot robot){

        this.socket = socket;
        this.robot = robot;
        start(); //Start the thread and hence calling run method
        }



        public void run(){
        Scanner scanner = null;
        try {
        scanner = new Scanner(socket.getInputStream());
        while(continueLoop){
        //recieve commands and respond accordingly

        int command = scanner.nextInt();
        switch(command){
                case-1:
                robot.mousePress(scanner.nextInt());
                break;
                case-2:
                robot.mouseRelease(scanner.nextInt());
                break;
                case-3:
                robot.keyPress(scanner.nextInt());
                break;
                case-4:
                robot.keyRelease(scanner.nextInt());
                break;
                case-5:
                robot.mouseMove(scanner.nextInt(),scanner.nextInt());
                break;
                }

                }
        }catch(IOException ex){
                ex.printStackTrace();
                }
            }//end function
            
            }//end class

Client Side:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JPanel;


class SendEvents implements KeyListener, MouseMotionListener, MouseListener{
    private Socket cSocket = null;
    private JPanel cPanel = null;
    private PrintWriter writer = null;
    String width = "", height = "";
    double w;
    double h;

SendEvents(Socket s, JPanel p, String width, String height){
    cSocket = s;
    cPanel = p;
    this.width = width;
    this.height = height;
    w = Double.valueOf(width.trim()).doubleValue();
    h = Double.valueOf(width.trim()).doubleValue();

    //Associate event listeners to the panel

    cPanel.addKeyListener(this);
    cPanel.addMouseListener(this);
    cPanel.addMouseMotionListener(this);

    try{
        //Prepare PrintWriter which will be used to send commands to the client
        writer = new PrintWriter(cSocket.getOutputStream());
        } catch(IOException ex) {
        ex.printStackTrace();
    }
}

public void mouseDragged(MouseEvent e){
}

public void mouseMoved(MouseEvent e){
    double xScale = (double)w/cPanel.getWidth();
    double yScale = (double)h/cPanel.getHeight();
    writer.println(Commands.MOVE_MOUSE.getAbbrev());
    writer.println((int)(e.getX()*xScale));
    writer.println((int)(e.getY()*yScale));
    writer.flush();
}

public void mouseClicked(MouseEvent e){
}

public void mousePressed(MouseEvent e){
    writer.println(Commands.PRESS_MOUSE.getAbbrev());
    int button = e.getButton();
    int xButton = 16;
    if(button==3){
        xButton = 4;
    }
    writer.println(xButton);
    writer.flush();
}

public void mouseReleased(MouseEvent e){
    writer.println(Commands.RELEASE_MOUSE.getAbbrev());
    int button = e.getButton();
    int xButton = 16;
    if(button==3){
        xButton = 4;
    }
    writer.println(xButton);
    writer.flush();
}

public void mouseEntered(MouseEvent e){
}

public void mouseExited(MouseEvent e){
}

public void keyTyped(KeyEvent e){
}

public void keyPressed(KeyEvent e){
    writer.println(Commands.PRESS_KEY.getAbbrev());
    writer.println(e.getKeyCode());
    writer.flush();
}

public void keyReleased(KeyEvent e){
    writer.println(Commands.RELEASE_KEY.getAbbrev());
    writer.println(e.getKeyCode());
    writer.flush();
}

My question is about client side, they are sending over writer.println(Commands.______.getAbbrev()); Does this line of code have something to do with the switch and case on serverside code?

java

networking

switch-statement

remote-desktop

0 Answers

Your Answer

Accepted video resources