I'm trying to send some bytes of data to login to an embedded device over my lan but when I send the data there is 2 bytes being appended to the tcp stream.
What is causing this?
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();
}
}
console output:
6745000000000000000000004b4c4c29
4d15176d0000000000000000005d4c57
751653543b0808001c00000000001b00
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
wireshark output:
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 ..
It's println
which is adding the \r\n
, but you've got fundamental problems here beyond that. You're treating binary data as if it's text. That will bite you sooner or later.
If you're trying to work with binary data, use OutputStream
and InputStream
subclasses - not Writer
and Reader
subclasses. You're already getting streams from the socket - why are you wrapping them in PrintWriter
and PrintReader
? That's a fundamental mistake which you should fix immediately.
As soon as you've stopped doing that, you won't end up with extra "line break" data, as there's no such thing as a line break when you're not thinking about text. See Marc Gravell's blog post on IO mistakes for more on this.
Additionally, I would strongly advise you to start following Java naming conventions - methods are camelCased
, not PascalCased
. I hope that in real code your exception handling is somewhat different, too :)
See more on this question at Stackoverflow