I need a byte buffer to be sent over TCP. I need a way of efficiently determining the amount of bytes created from encoding something like a string.
There would be no need for this, if i simply used this code.
byte[] buffer = encoder.GetBytes("Hello Client!");
clientStream.Write(buffer, 0 , buffer.Length);
But the problem is, i'm going to be sending multiple messages one after another, and this code allocates memory for a byte buffer every time i want to send a message. It's my understanding that his is inefficient / slow because it allocates memory every time.
What I want to do is just create a large byte buffer, and write all my messages to it, and send only part of the array that has the message. But I can't find a way of efficiently doing this. ASCII.Encoding.Getbytes(string) will just return the byte array and put it into my large byte buffer, starting from position 0. I need the length of the bytes of the message put into the byte buffer, without having to call the getbytes(string).Length, because this encodes it again, which is inefficient.
There is probably some obvious solution to this that I can't find.