vb.net 清除网络流中的数据而不处理它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14280752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:52:20  来源:igfitidea点击:

Clear The Data In A Network Stream Without Disposing It

.netvb.netnetworkstream

提问by Ryklon Zen

How can you clear the data in a network stream without disposing it?

如何在不处理的情况下清除网络流中的数据?

Dim ns As NetworkStream

Edit:

编辑:

Based on the comment below by James, it seems that this question is not yet answered properly.

根据James下面的评论,这个问题似乎还没有得到正确回答。

I thought doing:

我想这样做:

ns = Nothing

would clear the network stream, but I was wrong.

会清除网络流,但我错了。

回答by Jim Mischel

I assume by "clear the network stream," you mean you want to empty the input buffer?

我假设“清除网络流”是指要清空输入缓冲区?

var buffer = new byte[4096];
while (ns.DataAvailable)
{
    ns.Read(buffer, 0, buffer.Length);
}

That throws away all the data, but leaves the stream open for reading.

这会丢弃所有数据,但让流打开以供读取。

If you mean that you want to clear the output buffer (on a writeable stream), I have no idea. You'll probably have to close the stream and create a new one.

如果您的意思是要清除输出缓冲区(在可写流上),我不知道。您可能必须关闭流并创建一个新流。