Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 113997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:47:26+00:00 2026-05-11T02:47:26+00:00

I’ve been doing a lot of research on how best to write correct network

  • 0

I’ve been doing a lot of research on how best to write ‘correct’ network code in C#.

I’ve seen a number of examples using the ‘using’ statement of C#, and I think this is a good approach, however i’ve seen inconsistent use of it with various expressions.

For instance, suppose I have some code like this:

TcpClient tcpClient = new TcpClient('url.com', 80); NetworkStream tcpStream = tcpClient.GetStream(); StreamReader tcpReader = new StreamReader(tcpStream); StreamWriter tcpWriter = new StreamWriter(tcpStream); 

Obviously, this code is going to be very flaky. So, i’ve seen some code that puts the using on the tcpClient, which seems good. However, doesn’t NetworkStream also have resources that need cleaning up? What about StreamReader/Writer?

Do I need to wrap all 4 statements in nested using statements?

And if so, what happens when the time has come to dispose? Won’t StreamWriter close the stream and consequently the socket? Then what happens when StreamReader, then NetworkStream, then TcpClient each go through their disposals?

Which brings up another question. WIth both StreamReader and StreamWriter composed of the same stream, who owns it? Don’t they both think they own it, and will thus both try to destroy it? Or does the framework know that the stream has already been destroyed and just silently ignore it?

It almost seems like the using statement is only necessary for the last statement in the chain, but then what happens if an exception is thrown in GetStream()? I don’t think it would properly clean up the socket then, so it seems redundant usings are necessary to ensure this doesn’t happen.

Does anyone know of any good, recent books on network programming with .net, and prefeably c# that include chapters on exception handling and resource management? Or maybe any good articles online? All the books I can find are from the .NET 1.1 era (Network Programming for the Microsoft .NET Framework, Network Programming in.NET, etc..), so this seems like a topic that needs some good resources.

EDIT:

Please, don’t let Marc’s very good comment stop anyone else from commenting on this 🙂

I’d like to hear anyone elses book recommendations or opinions on resource management, especially in regard to asynchronous usage.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:47:27+00:00Added an answer on May 11, 2026 at 2:47 am

    Generally, objects should internally handle multiple Dispose() calls, and only do the main code once; so a stream getting Dispose()d multiple times is not usually a problem. Personally, I would use lots of using there; note that you don’t need to indent/nest, though (unless different levels have different life-times):

    using(TcpClient tcpClient = new TcpClient('url.com', 80)) using(NetworkStream tcpStream = tcpClient.GetStream()) using(StreamReader tcpReader = new StreamReader(tcpStream)) using(StreamWriter tcpWriter = new StreamWriter(tcpStream)) {    ... } 

    As you say, this ensures that if an error happens during initialization, everything is still cleaned up correctly. This also ensures that every level gets a chance (in the right order) to deal correctly with any buffered data, etc.

    Re ownership; NetworkStream is actually an oddity in the first place… most streams are either input xor output. NetworkStream bends a few rules and shims two directions into one API; so this is an exception… normally the ownership would be clearer. Additionally, many wrappers have a flag to determine whether they should close the wrapped stream. StreamReader doesn’t, but some do (such as GZipStream, which has a leaveOpen ctor option). If you don’t want to flow ownership, this is an option – or use a non-closing stream intermediary – one is here (NonClosingStream or similar).

    Re books; I picked up a copy of ‘TCP/IP Sockets in C#: Practical Guide for Programmers’ (here) – adequate, but not great.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.