One disadvantage of using the .NET Framework implementation of NetworkStream::Write
is that if the underlying network (OSI layers 1-4) is unable to receive the entire data buffer you are not made aware (except, perhaps, by inspecting .NET Networking performance counters.)
Unreliable.
Something like this appears in NetworkStream::Write
of Microsoft's .NET Framework System.dll:
try
{
socket.Send(buffer, offset, size, SocketFlags.None);
}
/* ... catch{throw;} */
Note how the return value, which represents the count of bytes written by Send()
, is discarded. This suggests NetworkStream::Write
is unreliable for sub-par networks (roaming/wireless), or networks that may become IO bound (exceeding available bandwidth.)
More than one implementation.
You can find other implementations which write all bytes until all of buffer
has been written (or other failure occurs.) It is a standard method of sending data, and will always exhibit the following behavior:
var count = 0;
while (count < size)
{
count += socket.Send(buffer, offset + count, size - count, ...);
}
What does this mean?
You can write proper and reliable send() code by calling Socket::Send
directly, you cannot write proper and reliable send() code calling NetworkStream::Write
.
As an aside, other variations of NetworkStream
do not have the same problem the BCL disassembly shows (see mono,cosi2), and still others show similar problems despite their best efforts (see flourinefx).
References
- Mono Repository on Github
- FlourineFxSL Source Code
- cosi2 Repository on BitBucket (via searchcode.com)