Does NetworkStream.DataAvailable see buffered data?

前端 未结 2 373
耶瑟儿~
耶瑟儿~ 2021-01-13 13:05

Does NetworkStream.DataAvailable 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 t

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 14:10

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

    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.

    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.

提交回复
热议问题