NetworkStream doesn't support seek operations

前端 未结 4 542
鱼传尺愫
鱼传尺愫 2021-01-24 02:21

I\'m creating simple proxy server but I faced a strange situation, I\'ve following code :

var clientRequestStream = _tcpClient.GetStream();
var requestHeader = c         


        
4条回答
  •  梦谈多话
    2021-01-24 02:34

    Assuming you don't want to properly rewrite your GetUtf8String method to not require seek...

    The easiest approach would be to copy stream to MemoryStream first and than you have stream you can seek as much as you want. Stream.CopyTo will take care of copying (.Net4+)

    Note that you need to seek resulting MemoryStream back by setting Position property to 0 (copiedStream.Position=0) or cloning the stream by creating new one (preferably read only) based on buffer and length of first memory stream.

提交回复
热议问题