Copying NetworkStream to MemoryStream takes infitity ∞ days

后端 未结 3 628
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 04:54

I\'ve following code:

_clientRequestStream = _tcpClient.GetStream();

var memoryStream = new MemoryStream();
_clientRequestStream.CopyTo(memoryStream);
         


        
3条回答
  •  难免孤独
    2021-01-20 05:40

    A network stream remains open until it is closed by one end of the stream. CopyTo() copies all data from the stream, waiting until the stream ends. If the server is not sending data, the stream does not end or close and CopyTo() dutifully waits for more data or for the stream to end. The server on the other end of the stream must close the stream for it to end and CopyTo() to return.

    Google "TcpClient Tutorial" or "TcpCLient Sample" to get some good pages showing other ways you might use them, such as checking NetworkStream.DataAvailable to see if there is data waiting or if the stream is still open with no data. To just read some data and not wait for the stream to close you would use NetworkStream.Read() or wrap it in a StreamReader and use ReadLine(). It all depends on the server you are connecting to and what you are trying to accomplish.

提交回复
热议问题