Reputation: 127
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.
Upvotes: 0
Views: 144
Reputation: 6060
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.
Upvotes: 2