I have some C# that is successfully calling my TCP service (running on Node.js) and receiving a response. My code looks similar to this:
string html = Console.ReadLine();
Byte[] htmlBytes = System.Text.Encoding.ASCII.GetBytes(html);
TcpClient client = new TcpClient("127.0.0.1", 5000);
NetworkStream stream = client.GetStream();
stream.Write(htmlBytes, 0, htmlBytes.Length);
Console.WriteLine("Message has been sent");
Byte[] responseBytes = new Byte[50000];
int numberOfBytes = stream.Read(responseBytes, 0, responseBytes.Length);
My questions is in regarding to this code:
Byte[] responseBytes = new Byte[50000];
int numberOfBytes = stream.Read(responseBytes, 0, responseBytes.Length);
Because responseBytes
is passed by reference into stream.Read(...)
, it must be initialized, so I've chosen a size of 50000. If the response is more than 50000, then I only receive the first 50000 bytes.
This works fine if my response is 50000 bytes or smaller, but what if I don't know what the response size will be? Is there a best practice for receiving large responses or handling a situation where the byte array returned is unknown?