During network communication, one party needs to transmit data to the other. The data can be values, one statement, and an array.
In C #, the socket class is used for communication. The send (byte [] B) parameter is in the byte array format, that is, when sending
You must convert the data you want to send to a byte array before sending the data.
To get data in the byte array.
So how does the receiving end know the significance of the byte values in the received byte array? Of course you can know, because
Protocol!
A Data Protocol roughly stipulates the following: data format, data Header Format, data content, and data tail representation.
Partially. In a word, it is a description of the data content and organization method.
The data sender works on the security protocol to compile the data. In this byte array, 0-3 bytes are the data header, and 4-5 bytes are the data.
Length, 6-26 bytes for content, and 27-28 for verification code.
NMEA-0183 protocol data: $ gpgga, 134658.00, 5106.9792, N, 11402.3003, W, 1.0, 1048.47, 16.27, M,-, M, 08, AAAA * 60
The recipient already knows how to parse the data, So how should the sender organize the data?
How can we add existing data to some specified locations in the byte array according to protocol rules?
Different Languages have their own implementation methods. Taking C # as an example, you can use the generic byte list method:
1. Define a conversion buffer.
List<byte> buffer = new List<byte>();
2. Convert the data into a byte array and store it in the buffer zone. multiple types of data can be saved.
float HeightD = 1.2616f;buffer.AddRange(BitConverter.GetBytes(HeightD));
3. Convert the data in the buffer to a byte array
byte[] message = new byte[length];buffer.CopyTo(0, message, 0, length)
At this point, the byte array sent by message is obtained.
I have not tried to define a struct object, assign values to the struct object attribute, and then retrieve the byte array from the object's memory address.
This is just one of my own methods. I urge the passing experts to specify the true meaning of data compilation.