简体   繁体   中英

Dumping data from incoming NetworkStream?

Is there a way to somehow skip/dump X bytes of data from incoming NetworkStream? 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.

Currently I am using ReadAsync() method to read the stream.

No, you must read all of the data in a 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. 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.

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 . It of course, under the table, would have to read and ignore the sections you used Seek or Position to bypass; and unless you buffer it, you wouldn't be able to implement Seek or Position backwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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