网络编程之屏幕广播教师端

129 阅读4分钟
工具类
[AppleScript]
纯文本查看
复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
package cn.itcast.screenbroadcast;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
public class Util {
//
private static Robot robot ;
//
static{
try {
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* ץͼ
*/
public static byte[] captureScreen(){
try {
BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 1366, 768));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
public static byte[] long2Bytes(long l){
byte[] bytes = new byte[8] ;
bytes[0] = (byte)l ;
bytes[1] = (byte)(l >> 8) ;
bytes[2] = (byte)(l >> 16) ;
bytes[3] = (byte)(l >> 24) ;
bytes[4] = (byte)(l >> 32) ;
bytes[5] = (byte)(l >> 40) ;
bytes[6] = (byte)(l >> 48) ;
bytes[7] = (byte)(l >> 56) ;
return bytes ;
}
public static long byte2Long(byte[] bytes){
return ((long)(bytes[0] & 0xFF))
| ((long)(bytes[1] & 0xFF) << 8)
| ((long)(bytes[2] & 0xFF) << 16)
| ((long)(bytes[3] & 0xFF) << 24)
| ((long)(bytes[4] & 0xFF) << 32)
| ((long)(bytes[5] & 0xFF) << 40)
| ((long)(bytes[6] & 0xFF) << 48)
| ((long)(bytes[7] & 0xFF) << 56);
}
public static byte[] int2Bytes(int l){
byte[] bytes = new byte[8] ;
bytes[0] = (byte)l ;
bytes[1] = (byte)(l >> 8) ;
bytes[2] = (byte)(l >> 16) ;
bytes[3] = (byte)(l >> 24) ;
return bytes ;
}
public static int bytes2Int(byte[] bytes){
return ((bytes[0] & 0xFF))
| ((bytes[1] & 0xFF) << 8)
| ((bytes[2] & 0xFF) << 16)
| ((bytes[3] & 0xFF) << 24);
}
public static int byte2Int(byte[] bytes,int offset){
return ((bytes[0 + offset] & 0xFF))
| ((bytes[1 + offset] & 0xFF) << 8)
| ((bytes[2 + offset] & 0xFF) << 16)
| ((bytes[3 + offset] & 0xFF) << 24);
}
public static byte[] zipData(byte[] rawData) {
try {
//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry(new ZipEntry("one"));
zos.write(rawData);
zos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] unzipData(byte[] zipData) {
try {
//
ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
ZipInputStream zis = new ZipInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
zis.getNextEntry();
byte[] buf = new byte[1024];
int len = 0 ;
while((len = zis.read(buf)) != -1){
baos.write(buf, 0, len);
}
zis.close();
bais.close();
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}


幁单元实体类
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
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
package cn.itcast.screenbroadcast;
public class FrameUnit {
private long frameId;
private int unitCont;
private int unitNo;
private int dateLen;
private byte[] data;
public long getFrameId() {
return frameId;
}
public void setFrameId(long frameId) {
this.frameId = frameId;
}
public int getUnitCont() {
return unitCont;
}
public void setUnitCont(int unitCont) {
this.unitCont = unitCont;
}
public int getUnitNo() {
return unitNo;
}
public void setUnitNo(int unitNo) {
this.unitNo = unitNo;
}
public int getDateLen() {
return dateLen;
}
public void setDateLen(int dateLen) {
this.dateLen = dateLen;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}

发送工具类
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
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
package cn.itcast.screenbroadcast;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.List;
public class ScreenBroadCastSender {
private InetSocketAddress bcAddr ;
private DatagramSocket socket ;
public ScreenBroadCastSender(){
try {
socket = new DatagramSocket(8888);
bcAddr = new InetSocketAddress("192.168.12.255", 9999);
} catch (Exception e) {
e.printStackTrace();
}
}
public void send(List<FrameUnit> unitList){
try {
for(FrameUnit unit : unitList){
byte[] packetData = popFrameUnit(unit);
DatagramPacket pack = new DatagramPacket(packetData, packetData.length);
pack.setSocketAddress(bcAddr);
socket.send(pack);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private byte[] popFrameUnit(FrameUnit unit){
byte[] bytes = new byte[unit.getDateLen() + 14] ;
byte[] frameIdBytes = Util.long2Bytes(unit.getFrameId());
System.arraycopy(frameIdBytes, 0, bytes, 0, 8);
bytes[8] = (byte)unit.getUnitCont();
bytes[9] = (byte)unit.getUnitNo();
byte[] dateLenBytes = Util.int2Bytes(unit.getDateLen());
System.arraycopy(dateLenBytes, 0, bytes, 10, 4);
System.arraycopy(unit.getData(), 0, bytes, 14, unit.getDateLen());
return bytes ;
}
}

更多技术资讯可关注:gzitcast