I have a custom protocol to send and receive messages over TCP like the following:
The first 4 bytes is the message type, the following 4 bytes is the length of the message and the rest of the buffer is containing the message itself.
private byte[] CreateMessage(int mtype,string data)
{
byte[] buffer = new byte[4 + 4 + data.Length];
//write mtype, data.Length, and data to buffer
return buffer;
}
I want to write mtype to the first 4 bytes of buffer and then data.Length to next 4 bytes and then the data. I am coming from golang world and we do that like the following:
buf := make([]byte, 4+4+len(data))
binary.LittleEndian.PutUint32(buf[0:], uint32(mtype))
binary.LittleEndian.PutUint32(buf[4:], uint32(len(data)))