-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientMain.java
184 lines (137 loc) · 4.93 KB
/
ClientMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.awt.*; // robot class
import java.awt.event.*;
import javax.swing.*; //joptionpane
import java.net.*; //socket
import java.io.*; //stream
import java.awt.image.*; // not used
import javax.imageio.*; //
import java.util.Scanner;
// main class of client
public class ClientMain {
Socket socket = null;
public static void main(String[] args){
//joptionpane is small window with a label
//showInoputDialog accepts user input and returns as string
String ip = JOptionPane.showInputDialog("Please enter server IP");
String port = JOptionPane.showInputDialog("Please enter server port");
new ClientMain().initialize(ip, Integer.parseInt(port));
}
public void initialize(String ip, int port ){
Robot robot ; //Used to capture the screen
Rectangle rectangle; //Used to represent screen dimensions
try {
System.out.println("Connecting to server ..");
socket = new Socket(ip, port);
System.out.println("Connection done.");
//Get screen dimensions
// dimension n rectangel class is present in awt package
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
rectangle = new Rectangle(dim);
//Prepare Robot object
robot = new Robot();
//ScreenSender sends screenshots of the client screen
new ScreenSender(socket,robot,rectangle);
//ServerCmdExecution recieves server commands and execute them
new ServerCmdExecution(socket,robot);
}
catch (Exception ex) {
///
}
}
}
//send sshots
class ScreenSender extends Thread {
Socket socket ;
Robot robot; // Used to capture screen
Rectangle rectangle; //Used to represent screen dimensions
boolean continueLoop = true; //Used to exit the program
public ScreenSender(Socket socket, Robot robot,Rectangle rect) {
this.socket = socket;
this.robot = robot;
rectangle = rect;
start(); //to start the thread
}
// overriding run() of Thread class
public void run()
{
ObjectOutputStream oos = null ; //Used to write an object to the streem
try{
//Prepare ObjectOutputStream
oos = new ObjectOutputStream(socket.getOutputStream());
/*
* Send screen size to the server in order to calculate correct mouse
* location on the server's panel
*/
oos.writeObject(rectangle);
}catch(Exception ex){
////
}
while(continueLoop){
//Capture screen
BufferedImage image = robot.createScreenCapture(rectangle);
// we acnnot send BufferedImage in stream
// so we converted it to imageicon
ImageIcon imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
oos.writeObject(imageIcon);
oos.reset(); //Clear ObjectOutputStream cache
System.out.println("New screenshot sent");
}
catch (Exception ex) {
////
}
//wait for 100ms to reduce network traffic
try{
Thread.sleep(100);
}
catch(Exception e){
/////
}
}
}
}
class ServerCmdExecution extends Thread {
Socket socket = null;
Robot robot = null;
boolean continueLoop = true;
public ServerCmdExecution(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 {
//prepare Scanner object
System.out.println("Preparing InputStream");
scanner = new Scanner(socket.getInputStream());
while(continueLoop){
//recieve commands and respond accordingly
System.out.println("Waiting for command");
int command = scanner.nextInt();
System.out.println("New command: " + command);
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 (Exception e) {
///
}
}
}