What are the possible causes for Stream not Writable Exception when serializing custom object over TCP using Network Stream in C#.
I am Sending the Mp3 data in the form of Packets.The Frame consists of Byte[] Buffer.I am Using Binary Formatter to serialize the object.
BinaryFormatter.Serialize(NetworkStream,Packet);
The Mp3 Played at client with distortion and jitters end for few seconds and then The above mentioned exception raised.I m using NAudio Open Source library for it.
Before doing this modification I was using
NetworkStream.Write(Byte[] Buffer,0,EncodedSizeofMp3);
and it was writing it successfully before giving any exception
If you are writing to a
NetworkStream, the stream/socket could be closedIf you are writing to a
NetworkStream, it could have been created withFileAccess.ReadIf I had to guess, though, it sounds like something is closing the stream – this can be the case if, say, a “writer” along the route assumes it owns the stream, so closes the stream prematurely. It is pretty common to have to write and use some kind of wrapper
Streamthat ignoresClose()requests (I have one in front of me right now, in fact, since I’m writing some TCP code).As a small aside; I generally advise against
BinaryFormatterfor comms (except remoting) – most importantly: it doesn’t “version” in a very friendly way, but it also tends to be a bit verbose in most cases.Here’s the wrapper I’m using currently, in case it helps (the
Reset()method spoofs resetting the position, so the caller can read a relative position):