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

查看:123
本文介绍了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执行查找操作,这也不例外,但是当我想从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中,然后就可以在您的内心世界中找到那个.在.NET 4中,使用 Stream.CopyTo :

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不可寻找是有道理的-它只是服务器向您提供的数据流.除非您可以告诉 server 快退(仅在某些情况下才有意义),否则无法进行搜索,除非某些内容缓冲了您需要快退的数据-这基本上就是复制到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天全站免登陆