-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayServer.java
105 lines (103 loc) · 2.58 KB
/
DisplayServer.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
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
public class DisplayServer extends Thread
{
ServerSocket ss = null;
ServerSocket admin = null;
Vector<ClientThread> clientList =new Vector<ClientThread>();
long sleepInterval = 125;
public DisplayServer(long interval) throws Exception {
ss = new ServerSocket(2016);
admin = new ServerSocket(2203);
sleepInterval = interval;
this.setPriority(MIN_PRIORITY);
this.start();
startServer();
}
private void startServer() {
Socket client = null;
System.err.println("Starts listening for clients");
ClientThread ct = null;
while(true) {
try {
client = ss.accept();
ct = new ClientThread(client);
clientList.addElement(ct);
ct.start();
} catch(Exception ex) {ex.printStackTrace();}
}
}
public void run() {
String localHost = "127.0.0.1";
try {localHost = InetAddress.getLocalHost().getHostAddress();}
catch(Exception ex) {}
while(true) {
try {
Socket ad = admin.accept();
if(ad.getInetAddress().getHostAddress().equals(localHost) &&
ad.getInputStream().read() == 'X') {
ss.close();
for(int i=0,n=clientList.size(); i<n; i++)
clientList.get(i).client.close();
System.exit(0);
}
} catch(Exception ex) {ex.printStackTrace();}
}
}
private void removeMe(Socket s) {
clientList.removeElement(s);
}
class ClientThread extends Thread {
Socket client = null;
ObjectOutputStream os = null;
InputStream is = null;
public ClientThread(Socket s) {
client = s;
try {
os = new ObjectOutputStream(s.getOutputStream());
is = s.getInputStream();
System.out.println("Client from "+s.getInetAddress().getHostAddress()+" connected");
} catch(Exception ex) {ex.printStackTrace();}
}
public void run() {
java.awt.image.BufferedImage img = null;
Robot r = null;
try { r = new Robot(); } catch(Exception ex) {ex.printStackTrace();}
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rect = new Rectangle(0,0,size.width, size.height);
javax.swing.ImageIcon icon = null;
while(true) {
try {
System.gc();
img = r.createScreenCapture(rect);
icon = new javax.swing.ImageIcon(img);
os.writeObject(icon);
os.flush();
icon = null;
System.gc();
try {Thread.currentThread().sleep(sleepInterval);} catch(Exception e) {}
}
catch(Exception ex) {
closeAll();
break;
}
}
}
private void closeAll() {
DisplayServer.this.removeMe(client);
try {
os.close();
is.close();
client.close();
} catch(Exception ex) { ex.printStackTrace(); }
}
}
public static void main(String arg[]) throws Exception {
long interval = 80;
if(arg.length == 1)
interval = Long.parseLong(arg[0]);
new DisplayServer(interval);
}
}