CommandClient.java
4.2 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
package com.sy.socket;
import com.sy.model.GatherInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
@Component
public class CommandClient {
private static final Logger logger = Logger.getLogger(CommandClient.class);
/**
* @Param GatherInfo 所传卡口信息
* @Param flag 是否放行标识符
* */
public static void Client(GatherInfo info,String flag) {
String xmlBody = getXmlInfo(info,flag);
Socket socket =null;
OutputStream op = null;
try {
socket = new Socket("192.168.1.107", 9002);
logger.info("socket通讯创建连接成功");
op = socket.getOutputStream();
byte[]xBody =xmlBody.getBytes("GB2312");
byte[] head = new byte[4];
head[0]=(byte)0xE2;
head[1]=(byte)0x5C;
head[2]=(byte)0x4B;
head[3]=(byte)0x89;
byte[]packge = intToByte4((40+xBody.length));
byte[] mType = new byte[1];
mType[0] = (byte)0x22;
byte[]station ="4257010001".getBytes("ASCII");
byte[]aisle ="4257011005".getBytes("ASCII");
byte[]eType ="E".getBytes("ASCII");
byte[] bwFlag = new byte[4];
bwFlag[0]=(byte)0xFF;
bwFlag[1]=(byte)0xFF;
bwFlag[2]=(byte)0xFF;
bwFlag[3]=(byte)0xFF;
byte[]xmlLength = intToByte4(xBody.length);
byte[]end = new byte[2];
end[0]=(byte)0xFF;
end[1]=(byte)0xFF;
byte[]b1 = byteMerger(head,packge);
byte[]b2 = byteMerger(mType,station);
byte[]b3 = byteMerger(aisle,eType);
byte[]b4 = byteMerger(bwFlag, xmlLength);
byte[]b5 = byteMerger(xBody, end);
byte[]b6 = byteMerger(b1,b2);
byte[]b7 = byteMerger(b3,b4);
byte[]b8 = byteMerger(b5,b6);
byte[]b9 = byteMerger(b7,b8);
op.write(b9);
op.close();
logger.info("发送完毕");
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
logger.info("创建连接失败"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.info("文件发送失败"+e.getMessage());
}
}
private static String getXmlInfo(GatherInfo info,String flag) {
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version='1.0' encoding='GB2312' ?>");
buffer.append("<COMMAND_INFO AREA_ID="+info.getAreaid()+" CHNL_NO="+info.getChnlno()+" I_E_TYPE="+info
.getIetype()+" SEQ_NO="+info.getSeqno()+">");
buffer.append("<CHECK_RESULT>11000000000000000000</CHECK_RESULT><OP_HINT>数据不完整</OP_HINT>");
buffer.append("<SEAL>");
buffer.append("<ESEAL_ID></ESEAL_ID>");
buffer.append("<SEAL_KEY></SEAL_KEY>");
buffer.append("<OPEN_TIMES></OPEN_TIMES>");
buffer.append("<ESEAL_IC_NO></ESEAL_IC_NO>");
buffer.append("</SEAL>");
buffer.append("<SZ_MSG></SZ_MSG>");
buffer.append("</COMMAND_INFO>");
return buffer.toString();
}
//int转byte
public static byte[] intToByte4(int i) {
byte[] targets = new byte[4];
//低位到高位
targets[0] = (byte) (i & 0xFF);
targets[1] = (byte) (i >> 8 & 0xFF);
targets[2] = (byte) (i >> 16 & 0xFF);
targets[3] = (byte) (i >> 24 & 0xFF);
return targets;
}
//合并两个byte数组
public static byte[] byteMerger(byte[] bt1, byte[] bt2) {
byte[] bt3 = new byte[bt1.length + bt2.length];
System.arraycopy(bt1, 0, bt3, 0, bt1.length);
System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
return bt3;
}
//byte转int
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位游
}
return value;
}
}