-
Notifications
You must be signed in to change notification settings - Fork 3
/
jiankong.java
330 lines (303 loc) · 9.69 KB
/
jiankong.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
*监控端代码
*
*/
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
public class jiankong{
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定端口的服务,从服务器端读取图像流,生成(刷新)管理面板
* 默认访问的端口为DEFAULT_PORT=30011 端口,
* 可以通过changePort来改变访问端口,也可以通过getPort查看当前访问端口
* 实例化线程类时需要传入一个ServerGUI类型的辅助窗体对象
*/
class WriteGUI extends Thread {
public static final int DEFAULT_PORT=30011;
private int port;
private ServerGUI rec;
/**
* @param rec 辅助窗体对象,可通过实例化获得
*/
public WriteGUI(ServerGUI rec) {
this.port=WriteGUI.DEFAULT_PORT;
this.rec = rec;
}
public void changePort(int port){
this.port=port;
}
public int getPort(){
return this.port;
}
public void run() {
while (rec.getBoo()) {
System.out.println((System.currentTimeMillis()/1000)%24%60);
Socket socket = null;
try {
socket = new Socket(rec.getIP(), this.port);
DataInputStream dis = new DataInputStream(socket.getInputStream());
ZipInputStream zis = new ZipInputStream(dis);
Image image = null;
try {
zis.getNextEntry();// 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
image = ImageIO.read(zis);// 把ZIP流转换为图片
rec.jlabel.setIcon(new ImageIcon(image));
rec.scroll.setViewportView(rec.jlabel);
rec.validate();
} catch (IOException ioe) {}
try{
// dis.close();
zis.close();
}catch (Exception e) {}
try {
TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} catch (IOException ioe) {
} finally {
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 访问指定主机的指定端口,向主机发送实例化线程类时传入的操控命令,实现对该主机的操控
* 默认访问服务端口为DEFAULT_PORT=30012 端口,主机IP为实例化线程类时传入的IP
* 可以通过changePort和changeIP来修改访问的端口和主机
* 也可以通过setOperateStr来设置需要发送的操控命令
* 需要注意的是,修改访问端口或主机必须在线程启动之前修改,否则修改无效
*/
class SendOperate extends Thread {
public static int DEFAULT_PORT=30012;
private String ip;
private int port;// 30012
private String operateStr;
public SendOperate(String ip, String operateStr) {
this.ip = ip;
this.port = SendOperate.DEFAULT_PORT;
this.operateStr = operateStr;
}
public void setOperateStr(String operateStr){
this.operateStr=operateStr;
}
public void changePort(int port){
this.port=port;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.ip=ip;
return true;
}
return false;
}
public int getPort(){
return this.port;
}
public String getIP(){
return this.ip;
}
public void run() {
if(this.operateStr==null||this.operateStr.equals("")){
return;
}
// if(this.operateStr.trim().startsWith("mouseMoved")){
// return;
// }
try {
Socket socket = new Socket(this.ip, this.port);
OutputStream os = socket.getOutputStream();
os.write((this.operateStr).getBytes());
os.flush();
socket.close();
System.out.println("INFO: 【SendOperate】ip=" + this.ip + ",port=" + this.port + ",operateStr=/" + " this.operateStr"+".");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @author LanXJ @doctime 2010-7-8
* 服务工具类
*/
class UtilServer{
public static boolean checkIp(String ip){
if(ip==null)return false;
String []dps=ip.split("//.");
if(dps.length!=4&&dps.length!=6)return false;
boolean isIp=true;
for (int i = 0; i < dps.length; i++) {
try {
int dp=Integer.parseInt(dps[i]);
if(dp>255||dp< 0){
throw new RuntimeException("error IP");
}
} catch (Exception e) {
isIp=false;
break;
}
}
return isIp;
}
}
/**
* @author LanXJ @doctime 2010-7-8
* serverManage的辅助窗体,内部事件封装了sendOperate的实现
*/
class ServerGUI extends JFrame {
private static final long serialVersionUID = 2273190419221320707L;
JLabel jlabel;
JScrollPane scroll;
private String ip;
private int port;
private boolean boo;
public boolean getBoo(){
return this.boo;
}
public int getPort(){
return this.port;
}
public void changePort(int port){
this.port=port;
}
public String getIP(){
return this.ip;
}
public boolean changeIP(String ip){
if(UtilServer.checkIp(ip)){
this.setTitle(this.getTitle().replace(this.ip, ip));
this.ip=ip;
return true;
}
return false;
}
protected ServerGUI(String IP, String sub) {
this.boo = true;
this.ip = IP;
this.port=SendOperate.DEFAULT_PORT;
this.setTitle("远程监控--IP:" + IP + "--主题:" + sub);
this.jlabel = new JLabel();
this.scroll = new JScrollPane();
this.scroll.add(this.jlabel);
scroll.addMouseListener(new MouseAdapter() {
/*public void mouseClicked(MouseEvent e) {// getMousePosition()
super.mouseClicked(e);
//由于加上单击事件后,鼠标按下并快速抬起 就设计到按下、抬起、单击 三个事件,将单击变为了双击
//所以不实现单击监听
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseClicked,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}*/
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mousePressed,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
@SuppressWarnings("static-access")
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
// int type = e.getModifiers();//e.BUTTON1_MASK 或 e.BUTTON2_MASK 或 e.BUTTON3_MASK
String operateStr ="mouseReleased,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
scroll.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseDragged,"+ x + "," + y + "," + e.getModifiers();
SendOperate sender=new SendOperate(ServerGUI.this.ip, operateStr);
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
int x = (int) e.getX() + (int) ServerGUI.this.scroll.getHorizontalScrollBar().getValue();
int y = (int) e.getY() + (int) ServerGUI.this.scroll.getVerticalScrollBar().getValue();
String operateStr ="mouseMoved,"+ x + "," + y;
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
String operateStr ="keyPress,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
String operateStr ="keyReleas,"+ e.getKeyCode();
SendOperate sender=new SendOperate(ServerGUI.this.ip, (operateStr));
sender.changeIP(ServerGUI.this.ip);//同步ip
sender.changePort(ServerGUI.this.port);//同步port
sender.start();
}
public void keyTyped(KeyEvent e) {
// super.keyTyped(e);
}
});
this.add(scroll);
this.setAlwaysOnTop(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(100, 75, (int) screenSize.getWidth() - 200, (int) screenSize.getHeight() - 150);
// this.setResizable(false);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 关闭窗体不做任何事
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
boo = false;
ServerGUI.this.dispose();
System.out.println("窗体关闭");
System.gc();
}
});
this.setVisible(true);
this.validate();
}
}