I am trying to make a tcp request in Java to a existing TCP server that is already available.
the interface specification is:
Field Length Type
Length 2 bytes 16-bits binary
Message ID 1 byte 8-bits binary
MSGTYPE 1 byte 8-bits binary
Variable1 4 bytes 32-bits binary
Variable2 30 bytes ASCII
Variable3 1 byte 8-bits binary
I understand how to convert a String to Binary using BigInteger.
String testing = "Test Binary";
byte[] bytes = testing.getBytes();
BigInteger bi = new BigInteger(bytes);
System.out.println(bi.toString(2));
My Understanding is that if i wanted to make a TCP request i would first
- need to convert each binary to a string
- append the values to a StringBuffer.
Unfortunately my understanding is limited so i wanted some advice on creating the TCP request correctly.
I wouldn’t use String (as you have binary data), StringBuffer (ever), or BigInteger (as it not what its designed for).
Assuming you have a Big Endian data stream I would use DataOutputStream
If you have a little endian protocol, you need to use ByteBuffer in which case I would use a blocking NIO SocketChannel.
BTW I would use
ISO-8859-1(8-bit bytes) rather thanUS-ASCII(7-bit bytes)