Reputation: 9522
Having code like
var stream = _tcpClient.GetStream();
Serializer.Serialize(stream, message);
Where Serialize
can possibly call Write
byte by byte I get a little affraid - when stream
will send data to server? will it lock to send on each byte? Shall I call flush
to ensure all data is sent? Would it be more effective to write to MemoryStream
and than Write entire Byte array to stream
?
Upvotes: 0
Views: 171
Reputation: 19031
You'll want to take a look at the NoDelay
property. As long as that's false (which is the default), TcpClient won't send a packet on every call to Write. It'll buffer up and only send a full packet when it feels the time is right. Some tweaking of SendBufferSize might also help.
NetworkStream.Flush
is documented to do nothing.
And as far as locking -- don't worry about that until your profiler has told you that's a problem. :-)
Upvotes: 2