简体   繁体   English

从传入的NetworkStream中转储数据?

[英]Dumping data from incoming NetworkStream?

Is there a way to somehow skip/dump X bytes of data from incoming NetworkStream? 有没有办法以某种方式从传入的NetworkStream中跳过/转储X字节的数据? You can't Seek it or Position it, so it seems only way is to copy it to other stream or just read it and dump it afterwards. 你不能Seek它或Position它,如此看来唯一的办法是将它复制到其他流或只是读它,之后倒掉。

Currently I am using ReadAsync() method to read the stream. 目前,我正在使用ReadAsync()方法读取流。

No, you must read all of the data in a NetworkStream . 不,您必须读取NetworkStream所有数据。 If you need to skip data, you can read and ignore it, but you have to read it before it moves forward. 如果需要跳过数据,则可以读取和忽略它,但是在继续前进之前必须先阅读它。 This is because NetworkStream is abstracting a TCP socket stream of data-- and there is nothing in TCP that says to skip bytes-- it's just a firehose of binary data coming at you. 这是因为NetworkStream正在抽象一个TCP套接字数据流-TCP中没有任何内容表明要跳过字节-这只是二进制数据的源泉。 Protocols on top of TCP, such as FTP or HTTP, may implement concepts that would allow you to position within a file or object, but NetworkStream isn't aware of all that-- it's just letting you get the socket data as it comes. TCP之上的协议(例如FTP或HTTP)可能会实现一些概念,这些概念使您可以在文件或对象中定位,但是NetworkStream并不了解所有这些信息-只是让您获得套接字数据。

If you have need of a Stream to abstract the seeking functions so you can pass it to some code that requires a seekable stream, you could build your own Stream class that wraps NetworkStream that implements Seek and or Position . 如果您需要Stream来抽象搜索功能,以便将其传递给需要可搜索流的代码,则可以构建自己的Stream类,该类包装实现Seek和or Position NetworkStream It of course, under the table, would have to read and ignore the sections you used Seek or Position to bypass; 当然,它必须在表格下读取并忽略您使用“ Seek或“ Position绕过的部分; and unless you buffer it, you wouldn't be able to implement Seek or Position backwards. 除非您对其进行缓冲,否则将无法向后实现“ Seek或“ Position

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM