In short, this is what I managed to set up:
Here is device 1:
import java.io.*; import java.net.*; import java.util.*; import java.nio.ByteBuffer; import java.math.BigInteger; class TCPClient { public static void main(String argv[]) throws Exception { Random rand = new Random(); int[] keys = {20, 1480, 70, 1520, 1470, 60, 0, -16, -15, -14}; float[] values = {0.000f, 0.000f, 0.000f, 0.000f, 0.006f, 0.000f, 0f, 22.150f, 744.301f, 41.136f}; byte[] bots = new byte[88]; byte[] one_byte = new byte[4]; //one_byte = intToByteArray(023, 1); bots[0] = intToByteArray(23, 1)[0]; bots[1] = intToByteArray(14, 1)[0]; for(int i=2, key_store = 0; i < 81; i++, key_store++) { one_byte = intToByteArray(keys[key_store], 2); // код ЗХВ bots[i++] = one_byte[0]; bots[i++] = one_byte[1]; one_byte = FloatToByteArray(values[key_store]); // отчет float bots[i++] = one_byte[0]; bots[i++] = one_byte[1]; bots[i++] = one_byte[2]; bots[i++] = one_byte[3]; } one_byte = intToByteArray(rand.nextInt(), 2); // код CRC 16 bots[86] = one_byte[0]; bots[87] = one_byte[1]; for(int i=0; i < 88; i++) { System.out.print(bots[i] + " "); } Socket socket = new Socket("localhost", 6789); DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream())); outToServer.write(bots); int bytesWritten = outToServer.size(); System.out.println("Total " + bytesWritten + " bytes are written to stream."); String modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); socket.close(); } public static byte[] FloatToByteArray(float value) { int bits = Float.floatToIntBits(value); byte[] bytes = new byte[4]; bytes[0] = (byte)(bits & 0xff); bytes[1] = (byte)((bits >> 8) & 0xff); bytes[2] = (byte)((bits >> 16) & 0xff); bytes[3] = (byte)((bits >> 24) & 0xff); return bytes; } public static byte[] intToByteArray(int value, int length) { if(length == 2) { byte[] data = new byte[2]; data[0] = (byte) (value & 0xFF); data[1] = (byte) ((value >> 8) & 0xFF); return data; } else { BigInteger bigInt = BigInteger.valueOf(value); return bigInt.toByteArray(); } } static int crc16(final byte[] buffer) { int crc = 0xFFFF; for (int j = 0; j < buffer.length ; j++) { crc = ((crc >>> 8) | (crc << 8) )& 0xffff; crc ^= (buffer[j] & 0xff);//byte to int, trunc sign crc ^= ((crc & 0xff) >> 4); crc ^= (crc << 12) & 0xffff; crc ^= ((crc & 0xFF) << 5) & 0xffff; } crc &= 0xffff; return crc; } }
How I think how it works: the first 2 bytes - I encode an int in byte and write it down. Next come the "D" structures containing 6 bytes each, where the first 2 bytes are the key and the remaining 4 are the value.
Well, at the end of the CRC code.
But what happens is that on device 2 I accept the request like this:
Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); while ((i = inFromClient.read()) != -1) { }
and I get an int value, when byte should come, I sent it to byte . What to do? Generally correct?