Flushing/closing streams
-
I have the following two chunks of code: 1) NetworkStream networkStream = new NetworkStream(member.socket); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); ... 2) NetworkStream networkStream = new NetworkStream(s); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(networkStream); ... What would be the order for closing the streams and flushing? And do I need to do both on both the client and server side? IE: 1) streamWriter.Flush(); streamWriter.Close(); networkStream.Flush(); networkStream.Close(); 2) bw.Flush(); bw.Close(); networkStream.Flush(); networkStream.Close(); Thanks
-
I have the following two chunks of code: 1) NetworkStream networkStream = new NetworkStream(member.socket); System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream); ... 2) NetworkStream networkStream = new NetworkStream(s); System.IO.BinaryWriter bw = new System.IO.BinaryWriter(networkStream); ... What would be the order for closing the streams and flushing? And do I need to do both on both the client and server side? IE: 1) streamWriter.Flush(); streamWriter.Close(); networkStream.Flush(); networkStream.Close(); 2) bw.Flush(); bw.Close(); networkStream.Flush(); networkStream.Close(); Thanks
You don't need to close the
NetworkStream
after closing theStreamWriter
, theStreamWriter
does it for you. MSDN says "Closes the current StreamWriter and the underlying stream". And closing theStreamWriter
flushes it, so you don't need toFlush ``before `Close` In short, all you need is streamWriter.Close(); And you must close it both on the client and the server, each end creates some resources that must be released by calling `Close`. Regards Senthil _____________________________ [My Blog](http://blogs.wdevs.com/senthilkumar) | [My Articles](http://www.codeproject.com/script/articles/list_articles.asp?userid=492196) | [WinMacro](http://geocities.com/win_macro)``