StreamWriter 在给定大消息时自动刷新缓冲区 [英] StreamWriter automatically flushing buffer when given large messages
问题描述
概述
我有一个客户端-服务器,其中我有 streamWriter.WriteLine(messageToClient);
在服务器上发送大消息.客户端上的 streamReader.ReadLine();
正在读取 2000 条长度的消息.
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.
这里的问题是 streamWriter.Flush()
对较大的消息没有影响,因为我的目标是服务器能够在直接刷新的缓冲区之间切换(具有流体服务器消息输出)而不是(服务器响应将被混合在一起,直到达到缓冲区限制以刷新).
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).
问题:我认为缓冲区已满会自动刷新?那么streamWriters 的最大缓冲区大小是多少?
Question: I am thinking that the buffers are auto flushing because they are full? Is so what is the streamWriters max buffer size?
我已经使用更小的 messageToClient
进行了测试,并且没有按预期刷新流.也就是服务器流一次向客户端输出一组小消息,大概是因为缓冲区已满.
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.
推荐答案
您是否尝试过使用 构造函数将缓冲区大小作为参数?
public StreamWriter(
Stream stream,
Encoding encoding,
int bufferSize
)
编辑:在反射器中寻找默认缓冲区大小为 1024 字节.
Edit: poking around in reflector it looks like the default buffer size is 1024 bytes.
这篇关于StreamWriter 在给定大消息时自动刷新缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!