forked from mir1198yusuf/remote_desktop_control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerMain.java
More file actions
282 lines (223 loc) · 7.36 KB
/
ServerMain.java
File metadata and controls
282 lines (223 loc) · 7.36 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
/**
* This is the entry class of the server
*/
public class ServerMain {
//Main server frame
private JFrame frame = new JFrame("SERVER MAIN");
//JDesktopPane represents the main container that will contain all
//connected clients' screens
private JDesktopPane desktop = new JDesktopPane();
public static void main(String args[]){
// showinputdialog will return user input as string
String port = JOptionPane.showInputDialog("Please enter listening port");
new ServerMain().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 ClientCreation
new ClientCreation(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);
}
}
class ClientCreation extends Thread {
JInternalFrame interFrame = new JInternalFrame("Client Screen",true, true, true);
JPanel cPanel = new JPanel();
Socket client;
JDesktopPane desktop;
ClientCreation(Socket client, JDesktopPane desktop) {
this.client = client;
this.desktop = desktop;
start();
}
/*
* Draw GUI per each connected client
*/
public void drawGUI(){
interFrame.setLayout(new BorderLayout());
interFrame.getContentPane().add(cPanel,BorderLayout.CENTER);
interFrame.setSize(100,100);
desktop.add(interFrame);
try {
//Initially show the internal frame maximized
interFrame.setMaximum(true);
} catch (Exception e) {
///
}
//this allows to handle KeyListener events
cPanel.setFocusable(true);
interFrame.setVisible(true);
}
public void run(){
//used to represent client screen size
Rectangle clientScreenDim = null;
//Used to read screenshots and client screen dimension
ObjectInputStream ois = null;
//start drawing GUI
drawGUI();
try{
//Read client screen dimension
ois = new ObjectInputStream(client.getInputStream());
clientScreenDim =(Rectangle) ois.readObject();
}catch(Exception e){
////
}
//Start recieveing screenshots
new ClientScreenReciever(ois,cPanel);
//Start sending events to the client
new ClientCommandsSender(client,cPanel,clientScreenDim);
}
}
class ClientScreenReciever extends Thread {
ObjectInputStream ois;
JPanel p;
boolean continueLoop = true;
public ClientScreenReciever(ObjectInputStream ois, JPanel p) {
this.ois = ois;
this.p = p;
//start the thread and thus call the run method
start();
}
public void run(){
try {
//Read screenshots of the client then draw them
while(continueLoop){
//Recieve client screenshot and resize it to the current panel size
ImageIcon imageIcon = (ImageIcon) ois.readObject();
System.out.println("New image recieved");
Image image = imageIcon.getImage();
image = image.getScaledInstance(p.getWidth(),p.getHeight()
,Image.SCALE_SMOOTH);
//Draw the recieved screenshot
Graphics graphics = p.getGraphics();
graphics.drawImage(image, 0, 0, p.getWidth(),p.getHeight(),p);
}
} catch (Exception e) {
////
}
}
}
class ClientCommandsSender implements KeyListener,
MouseMotionListener,MouseListener {
Socket s;
JPanel p ;
PrintWriter writer ;
Rectangle r ;
ClientCommandsSender(Socket s, JPanel p, Rectangle r) {
this.s = s;
this.p = p;
this.r = r;
//Associate event listners to the panel
p.addKeyListener(this);
p.addMouseListener(this);
p.addMouseMotionListener(this);
try {
//Prepare PrintWriter which will be used to send commands to
//the client
writer = new PrintWriter(s.getOutputStream());
} catch (Exception e) {
/////
}
}
//Not implemeted yet
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
// we find ratio between client screen n server screen size by dividing them
double xScale = r.getWidth()/p.getWidth();
System.out.println("xScale: " + xScale);
double yScale = r.getHeight()/p.getHeight();
System.out.println("yScale: " + yScale);
System.out.println("Mouse Moved");
writer.println(-5);
writer.println((int)(e.getX() * xScale));
writer.println((int)(e.getY() * yScale));
writer.flush();
}
//this is not implemented
public void mouseClicked(MouseEvent e) {
}
/* for getButton() methiod left mouse click = 1
right mouse click = 3
for robot class left mouse click = 16
right mouse click = 4
*/
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed");
writer.println(-1);
int button = e.getButton();
//first we assume left button is clicked
int xButton = 16;
if (button == 3) // if right button is clciked
{
xButton = 4;
}
// xbutton is value used to tell robot class which mouse button is pressed
writer.println(xButton);
writer.flush();
}
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released");
writer.println(-2);
int button = e.getButton();
System.out.println(button);
int xButton = 16;
if (button == 3) {
xButton = 4;
}
writer.println(xButton);
writer.flush();
}
//not implemented
public void mouseEntered(MouseEvent e) {
}
//not implemented
public void mouseExited(MouseEvent e) {
}
//not implemented
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed");
writer.println(-3);
writer.println(e.getKeyCode());
writer.flush();
}
public void keyReleased(KeyEvent e) {
System.out.println("Mouse Released");
writer.println(-4);
writer.println(e.getKeyCode());
writer.flush();
}
}
/*
PRESS_MOUSE(-1),
RELEASE_MOUSE(-2),
PRESS_KEY(-3),
RELEASE_KEY(-4),
MOVE_MOUSE(-5);
*/