难道NetworkStream.DataAvailable看到缓存的数据?缓存、数据、NetworkStream、DataAvailable

2023-09-04 00:46:39 作者:优越少年

是否NetworkStream.DataAvialable知道发送者的发送缓冲区是否为空?或者这仅仅表明接收器的读取缓冲区是否有数据?我的假设是后者......

Does NetworkStream.DataAvialable know whether the sender's send buffer is empty? Or does it simply indicate whether the receiver's read buffer has data? My assumption is the latter...

具体而言,对于一些涉及到正在进行的会话插座的工作,我目前使用的是长度 - preFIX让接收器知道到底有多少数据是在当前批处理;不过,我已经发了.patch建议我使用NetworkStream.DataAvailable代替。我担心的是,这只是告诉我是什么reciever已经得到了 - 不是发送者发送的原始数据 - 但我不是一个插座专家

Specifically, for some socket work involving an ongoing conversation, I currently use a length-prefix so the the receiver knows exactly how much data is in the current batch; however, I've been sent a .patch suggesting I use NetworkStream.DataAvailable instead. My concern is that this will just tell me what the reciever has got - not what the sender originally sent - but I'm not a sockets expert.

难道我错了吗?或者是与长度preFIX要走的路?

Am I wrong? Or is length-prefix the way to go?

(注意,我不能简单地阅读(),直到流被关闭,因为多批次发送在相同的连接,这是至关重要的,我把每个批次为独立的,如果我读了太多的一个批次(即使它得到缓冲和丢弃),那么对话将中断)。

(note I can't simply Read() until the stream is closed, since multiple batches are sent on the same connection, and it is vital that I treat each batch as separate; if I read too much in one batch (even if it gets buffered and discarded) then the conversation will break).

推荐答案

连接的一侧不会知道对方的发送缓冲区是否为空。

One side of a connection is not going to know whether the other side's send buffer is empty.

DataAvailable 只指示是否有读取输入的数据。你可以使用之前阅读(),但它本身并不能给你你想要的信息。它不会告诉你每个批次的开始和结束。

DataAvailable only indicates whether there is incoming data to be read. You could use that prior to Read(), but it alone doesn't give you the information you want. It doesn't tell you the beginning and ending of each batch.

我回来的往复对话之前,已经codeD,我用长度 - prefixes数据。我所做的是读取的字节一个确切的数字(大块的时间)写的辅助功能,并没有更多的。

I've coded back-and-forth conversation before, and I used length-prefixes in the data. What I did was write helper functions that read an exact number of bytes (chunks at a time) and no more.

流中的唯一替代长度间歇的值是检查传入数据和识别批次的开始与结束的某种方式。

The only alternative to length-of-batch values in the stream is some way of examining the incoming data and recognizing the beginnings and endings of batches.