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 becauseNetworkStream
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, butNetworkStream
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 wrapsNetworkStream
that implementsSeek
and orPosition
. It of course, under the table, would have to read and ignore the sections you usedSeek
orPosition
to bypass; and unless you buffer it, you wouldn't be able to implementSeek
orPosition
backwards.