Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Flushing/closing streams

Flushing/closing streams

Scheduled Pinned Locked Moved C#
sysadminquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    methodincharge
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • M methodincharge

      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

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      You don't need to close the NetworkStream after closing the StreamWriter, the StreamWriter does it for you. MSDN says "Closes the current StreamWriter and the underlying stream". And closing the StreamWriter flushes it, so you don't need to Flush ``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)``

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups