我正在尝试发送一些数据字节以通过我的局域网登录到嵌入式设备,但是当我发送数据时,有 2 个字节被附加到 tcp 流中。
这是什么原因造成的?
public class DeviceConnection {
Device dv;
Socket s;
PrintWriter out;
boolean connected = true;
public DvsConnection(Device d) {
try {
dv = d;
InetAddress addr = InetAddress.getByName(dv.IP_ADDRESS);
s = new Socket(addr, dv.ACTIVEX_PORT);
} catch (Exception ex) {
System.out.println(ex.toString());
connected = false;
}
Connect();
}
private void Connect() {
int[] headerStrt = {0x67, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int[] usrName = convert.EncryptStr(15, dv.LOGIN, cypher);
int[] password = convert.EncryptStr(15, dv.PASSWORD, cypher);
int[] headerEnd = {
0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
int[] messageArray = convert.concatAll(headerStrt, usrName, spacer2, password, headerEnd);
try {
out = new PrintWriter(s.getOutputStream(), true);
byte[] sbytes=convert.IntArray2ByteArray(messageArray);
String message=convert.ByteArray2Ascii(sbytes);
out.println(message);
String hex = convert.ByteArray2Hex(sbytes);
System.out.println(hex);
} catch (Exception ex) {
System.out.println(ex.toString());
connected = false;
}
}
public class Convert {
public String ByteArray2Hex(byte[] a) {
StringBuilder sb = new StringBuilder();
for (byte b : a) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
public String ByteArray2Ascii(byte[] data) {
StringBuilder sb = new StringBuilder(data.length);
for (int i = 0; i < data.length; ++i) {
if (data[i] < 0) {
throw new IllegalArgumentException();
}
sb.append((char) data[i]);
}
return sb.toString();
}
}
控制台输出:
6745000000000000000000004b4c4c29
4d15176d0000000000000000005d4c57
751653543b0808001c00000000001b00
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
线鲨输出:
00000000 67 45 00 00 00 00 00 00 00 00 00 00 4b 4c 4c 29 gE...... ....KLL)
00000010 4d 15 17 6d 00 00 00 00 00 00 00 00 00 5d 4c 57 M..m.... .....]LW
00000020 75 16 53 54 3b 08 08 00 1c 00 00 00 00 00 1b 00 u.ST;... ........
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00000070 0d 0a ..