NetworkStream 不支持查找操作 [英] NetworkStream doesn't support seek operations

查看:28
本文介绍了NetworkStream 不支持查找操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建简单的代理服务器,但遇到了一个奇怪的情况,我有以下代码:

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

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

GetUtf8StringStream 类的扩展方法,它读取流(包含 HttpRequest 标头).我需要提取这些标头以访问 Host 和 Requested Url.一旦读取 NetworkStream 完成.我需要执行查找操作并设置它的 clientRequestStream.Position = 0; 因为我必须读取该流并将其写入另一个远程 NetworkStream.

GetUtf8String is a extension method for Stream class which reads stream (contains HttpRequest headers). I need to extract those headers to access Host and Requested Url. Once reading NetworkStream is done. I need to perform seek operation and set its clientRequestStream.Position = 0; because I've to read that stream and write it on another remote NetworkStream.

我不知道该如何解决这个问题.任何建议都会有所帮助.

I don't know how should I solve this problem.Any advice will be helpful.

我也尝试将NetworkStream复制到MemoryStream,然后对MemoryStream执行seek操作,没有例外,但是当我想从NetworkStream读取时,它的缓冲区总是空的.

I also tried copy NetworkStream to MemoryStream then perform seek operation on MemoryStream, There is no exception but when I want to read from NetworkStream its buffer always is always empty.

我还使用反射器来查看 Stream.CopyTo 内部发生了什么.见以下代码:

Also I used reflector to see what happens inside Stream.CopyTo. See below code :

    private void InternalCopyTo(Stream destination, int bufferSize)
    {
        int num;
        byte[] buffer = new byte[bufferSize];
        while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
        {
            destination.Write(buffer, 0, num);
        }
    }

这就是 CopyTo 所做的.即使我使用 CopyTo 问题仍然没有解决.因为它将源(Here NetworkStream)读取到最后.我有其他方法来处理这种情况吗?

This is what CopyTo doing. Even if I use CopyTo Problem is still unresolved. Because it reads source (Here NetworkStream) to the end. I there another way to handle this situation?

推荐答案

你是从这个流中读到最后吗?如果是这样,我建议您将整个内容复制到 MemoryStream 中,然后您可以在 that 上查找您的内容.在 .NET 4 中,使用 Stream 尤其容易.复制到:

Are you reading from this stream until the end? If so, I suggest you just copy the entire contents into a MemoryStream, then you can seek on that to your heart's content. In .NET 4 it's particularly easy with Stream.CopyTo:

MemoryStream dataCopy = new MemoryStream();
using (var clientRequestStream = _tcpClient.GetStream())
{
    clientRequestStream.CopyTo(dataCopy);
}
dataCopy.Position = 0;
var requestHeader = dataCopy.GetUtf8String();

NetworkStream 不可搜索是有意义的——它只是服务器提供给您的数据流.除非您可以告诉 服务器 倒带(这仅在某些情况下才有意义),否则除非某些东西缓冲了您需要倒带的尽可能多的数据,否则无法寻找 - 这基本上就是复制到 MemoryStream 确实如此,以相当蛮力的方式.

It makes sense for NetworkStream not to be seekable - it's just a stream of data that a server is giving to you. Unless you can tell the server to rewind (which only even makes sense in some situations) there's no way of seeking unless something buffers as much data as you need to rewind - which is basically what copying to a MemoryStream does, in a pretty brute-force fashion.

这篇关于NetworkStream 不支持查找操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆