Reading from stream using coroutine

Before the first scene is loaded, I start a coroutine on an object which continuously reads bytes from a TcpClient network stream. The code is simple as follows:

while(true)
{
     packets = stream.Read(bytestoread, 0, 2048);
     yield return null;
}

While the code does exactly what I expect it to do, it effectively kills the game’s FPS. Stream.Read()
is apparently a slow method, and is likely not finished within a single frame.

My question is, how could I read from a stream constantly while the game is running, without of course causing the app to lag?

This question is way to abstract. A stream is an abstract concept. What Stream.Read actually does depends on the used implementation. For example a filestream will actually read from a file, a memory stream will read just from ram and a network stream will read from the associated network socket. Depending on what stream you’re using the Read method could block until data is available.

Any kind of Network traffic should be handled in a seperate thread. Also be aware that the Read method reads data in chunks of your specified buffer size. That means depending on the data you actually read the data could be splitted into two or more fragments.

You really need to add more information to your question.