StreamWriter automatically flushing buffer when given large messages

一个人想着一个人 提交于 2020-01-11 10:23:50

问题


Overview

I have a Client-Server whereby I have the streamWriter.WriteLine(messageToClient);on the server sending large messages. The streamReader.ReadLine(); on the client is reading the 2000 length messages fine.

The issue here is the streamWriter.Flush() has no affect on the larger messages, because what I am aiming for is the server to be able to switch between the buffers flushing directly (having a fluid server message output) and not (the server response would be jumbled together until it reaches the buffers limit to flush).

Question: I am thinking that the buffers are auto flushing because they are full? Is so what is the streamWriters max buffer size?

I have tested using much smaller messageToClient and the not flushing of the stream works as intended. Aka the server stream outputs a group of small messages at once to the client, presumably because the buffer was full.


回答1:


Have you tried using the constructor that takes the buffer size as an argument?

public StreamWriter(
    Stream stream,
    Encoding encoding,
    int bufferSize
)

Edit: poking around in reflector it looks like the default buffer size is 1024 bytes.



来源:https://stackoverflow.com/questions/5670873/streamwriter-automatically-flushing-buffer-when-given-large-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!