C#:实现NetworkStream.Peek? [英] C#: Implementing NetworkStream.Peek?

查看:292
本文介绍了C#:实现NetworkStream.Peek?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,还没有在C# NetworkStream.Peek 方法。什么是实施这种方法的最好方式,其功能就像 NetworkStream.ReadByte 除了返回字节不是从


解决方案

实际删除如果您不需要实际上检索字节,您可以参阅 DataAvailable 财产



否则,您可以使用的 的StreamReader 并调用它的皮克方法。



请注意,这些都不是从网络流读取特别可靠,由于延迟问题。数据可能变得可用(在读缓冲区存在)非常即时的之后的你偷看。



我不知道它是什么,你打算用这个做的,但的NetworkStream 方法是一个阻塞调用,所以你真的不需要检查状态,即使你在接受大块。如果你想保持应用程序响应而从流中读取,你应该使用一个线程或异步调用来接收数据,而不是



编辑:根据<一个HREF =htt​​p://stackoverflow.com/questions/1412852/streamwriter-wont-flush-to-networkstream>这个帖子, StreamReader.Peek 是越野车在的NetworkStream ,或至少已无证行为,所以要小心,如果你选择走这条路。






更新 - 回应评论



实际流偷看的概念本身实际上是不可能的;它只是一个流,并且一旦接收到字节那么它是在流不再。有些流寻求支持,所以你可以在技术上重新读取字节,但的NetworkStream 是不是其中之一。



偷看仅当读取该流到缓冲区适用;一旦数据在缓冲区中,然后偷看很简单,因为你只是检查在缓冲区的当前位置是什么。这就是为什么的StreamReader 是能够做到这一点;没有类一般会有自己的皮克方法。



现在,对于这个问题具体地讲,我怀疑这是否是真正正确的答案。我理解的动态选择用于处理流的方法的想法,但你的真正的需要做到这一点对原始流?您无法读取流转换为字节数组第一,甚至可以将其复制到的MemoryStream ,并对其进行处理,从一点上?



主要的问题我看到的是,如果当你从网络流读取坏事发生时,数据已经一去不复返了。但是,如果你第一次读入一个临时位置,可以调试此。你可以找出哪些数据是,为什么这是尝试处理的数据对象失败一半。



在一般情况下,你想要做的第一件事情一个的NetworkStream 被读入本地缓冲区。我能想到的不是唯一的原因要做到这一点,如果你正在阅读的数据了很多 - 即使这样,我可能会考虑使用文件系统,如果它不适合在内存中间缓冲器。

我不知道您的具体要求,但是从我学会为止,我的建议是:不要试图直接从<$ C处理数据$ C>的NetworkStream 除非有令人信服的理由这样做。考虑读取数据到内存或磁盘上,然后再处理副本。


Currently, there isn't a NetworkStream.Peek method in C#. What is the best way of implementing such a method which functions just like NetworkStream.ReadByte except that the returned byte is not actually removed from the Stream?

解决方案

If you don't need to actually retrieve the byte, you can refer to the DataAvailable property.

Otherwise, you can wrap it with a StreamReader and invoke its Peek method.

Note that neither of these are particularly reliable for reading from a network stream, due to latency issues. The data might become available (present in the read buffer) the very instant after you peek.

I'm not sure what it is that you intend to do with this, but the Read method on NetworkStream is a blocking call, so you don't really need to check for status, even if you are receiving in chunks. If you are trying to keep the application responsive while reading from the stream, you should use a thread or asynchronous call to receive the data instead.

Edit: According to this post, StreamReader.Peek is buggy on a NetworkStream, or at least has undocumented behaviour, so be careful if you choose to go that route.


Updated - response to comments

The notion of a "peek" on the actual stream itself is actually impossible; it's just a stream, and once the byte is received then it is no longer on the stream. Some streams support seeking so you could technically re-read that byte, but NetworkStream isn't one of them.

Peeking only applies when are reading the stream into a buffer; once the data is in a buffer then peeking is easy because you just check whatever's at the current position in the buffer. That's why a StreamReader is able to do this; no Stream class will generally have its own Peek method.

Now, for this problem specifically, I question whether or not this is really the right answer. I understand the idea of dynamically selecting a method for processing the stream, but do you really need to do this on the raw stream? Can you not read the stream into a byte array first, or even copy it into a MemoryStream, and process it from that point on?

The main issue I see is that if something bad happens when you're reading from a network stream, the data is gone. But if you read it into a temporary location first, you can debug this. You can find out what the data was and why the object that was trying to process the data failed halfway through.

In general, the very first thing you want to do with a NetworkStream is read it into a local buffer. The only reason I can think of not to do this is if you're reading an enormous amount of data - and even then, I might consider using the file system as an intermediate buffer if it won't fit in memory.

I don't know your exact requirements, but from what I've learned so far, my advice would be: Don't try to process your data directly from the NetworkStream unless there is a compelling reason to do so. Consider reading the data into memory or onto disk first, then processing the copy.

这篇关于C#:实现NetworkStream.Peek?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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