Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

c# - StreamWriter automatically flushing buffer when given large messages

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...