Copying NetworkStream to MemoryStream takes infitity ∞ days

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

I\'ve following code:

_clientRequestStream = _tcpClient.GetStream();

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


        
3条回答
  •  伪装坚强ぢ
    2021-01-20 05:53

    This will block indefinitely I believe until the underlying connection is closed, in which case I assume it would throw an IOException. Be careful with thinking of NetworkStream like a normal Stream. TCPClient.GetStream() is a nice abstraction which saves you the hassle of using Socket directly, but it's easy to fall into traps like these using synchronous operations with NetworkStream. It's worth checking out the specific MSDN documentation on NetworkStream.

    Here's a nice example of using async sockets as opposed to TCP client: http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

提交回复
热议问题