0

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

  1. need to convert each binary to a string
  2. append the values to a StringBuffer.

Unfortunately my understanding is limited so i wanted some advice on creating the TCP request correctly.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Gigaquad
  • 153
  • 4

2 Answers2

3

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

DataOutptuStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

out.writeShort(length);
out.write(messageId);
out.write(msgtype);
out.write((var1+"\0\0\0\0").substring(0, 4).getBytes("ISO-8859-1"));
out.write(var2.getBytes("ISO-8859-1"));
out.write(var2);

out.flush(); // optionally.

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 than US-ASCII (7-bit bytes)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks for the info, if the value for var1 is a string, how would i be able to convert the input into the bit (32 bit) format. So the bits dont matter? as the inputs originally would be string. – Gigaquad Oct 03 '12 at 12:44
  • I have added an example with padding and truncation if the string is less than or more than 4 bytes long. – Peter Lawrey Oct 03 '12 at 12:54
  • Thanks Peter, I was really heading the wrong way with my original thought process, so thanks for clearing things up. So if i wanted to have 16 bit and 32 bit strings converted then i would use getBytes("UTF-16") and getBytes("UTF-32"). is that correct? or is there an ISO equivalent? – Gigaquad Oct 03 '12 at 13:04
  • You can use those, but UTF-8 is likely to be a better choice. There might be an ISO equivalent but using UTF-n is likely to be clearer. – Peter Lawrey Oct 03 '12 at 13:09
0

You are going into the wrong direction. The fact that the message specification states that, for example, first field is a 16 bit binary doesn't mean that you will send a binary string. You will just send an (unsigned?) 16 bit number which will be, as a matter of fact, codified in binary since it's internal representation can just be that one.

When writing onto a socket through an DataOutputStream like in

int value = 123456;
out.writeInt(value);

you are already writing it in binary.

Jack
  • 131,802
  • 30
  • 241
  • 343