Currently I have been working on a program that receives data from the server using TcpClient. My problem is that whenever I read the stream until I can my client stops working, It just freezes. I would like to keep a continous connection to the server, but after my while function ran twice, the third loop doesn't execute read, and never returns anything.
As this answer states that I'm correct
How can I break or return this while loop, or somehow bypass the reading block?
using (NetworkStream stream = client.GetStream())
{
byte[] data = new byte[2048];
using (MemoryStream ms = new MemoryStream())
{
int numBytesRead;
while ((numBytesRead = stream.Read(data, 0, data.Length)) > 0)
{
ms.Write(data, 0, numBytesRead);
}
return ms.ToArray();
}
}