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 7683039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:42:20+00:00 2026-05-31T18:42:20+00:00

in server -(multi)client application [TCP]. I use Socket, NetworkStream, StreamReader and StreamWriter for each

  • 0

in server -(multi)client application [TCP]. I use Socket, NetworkStream, StreamReader and StreamWriter for each client i Accept in the server .. so i have couple of questions :

  • Do i have to dispose all of them when i want to close the connection with a client?
  • If the client Disposes the Socket that’s connected with the server .. do i also have do dispose that socket in the server side ,or it closes automatically ?
  • in my code here :

     Thread thAccept = new Thread(acceptClient);
     Thread thJob;
     private void acceptClient()
        {
            while (true)
            {
                Socket client = server.Accept();
                Console.WriteLine(client.RemoteEndPoint+" has connected");
                StreamReader reader = new StreamReader(new NetworkStream(client));
                //is it ok to create an instance NetworkStream like this or i will have to dispose it later?
    
                thJob = new Thread(Job);
                thJob.Start(reader);
            }
        }
    
     private void Job(object o)
        {
            StreamReader reader = (Socket)o;
            try
            {
                string cmd = null;
                while ((cmd = reader.ReadLine()) != null)
                {
                  //(BLA BLA..)
                }
            }
            catch
            {
                Console.WriteLine("Disconnected by catch");  
            }
            finally
            { 
                Console.WriteLine("Finally Done.");
                reader.Dispose();
            }
        }
    

    is that code fine to dispose all (needed to be disposed) objects?

    • 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. Editorial Team
      Editorial Team
      2026-05-31T18:42:21+00:00Added an answer on May 31, 2026 at 6:42 pm

      This is not a duplicate.

      Your code differs from the linked duplicate because in your code, the IDisposable is handed off to another thread.

      The general rule is that if you create an object that implements IDisposable, then you’re responsible for calling Dispose on it when you’re finished with it. When possible, that should be done in a using block, to ensure that Dispose is always called. In your case, your code is not finished with the object until the other thread is exited. In that thread, you correctly call Dispose in the finally block.

      If you had called Dispose on the NetworkStream, then it would have closed the StreamReader as well, which would defeat your purpose. I suspect it would be the same thing if you had called Dispose on the Socket. As such, your code is correct as-is.

      The object on the client side has no relationship with the object on the server side, except through TCP/IP. The fact that the client may call Dispose on its socket doesn’t mean that the server has to call Dispose on its socket. However, once the server is finished reading data from the socket, and the connection is closed, the server-side socket should be Disposed. I don’t know for certain, but I believe that when the StreamReader is disposed, the underlying NetworkStream will be disposed, which should call Dispose on the socket from which the stream was created.

      Your code is fine, except for some unrelated issues: you don’t need to set cmd to null, since you’re going to set the value in the next statement. Also, you should not use an empty catch block like that. You have no idea what exception was thrown, but you will ignore it anyway, without even logging or displaying the exception. At the least, you should do

          catch (Exception ex)
          {
      
              Console.WriteLine("Disconnected by exception " + ex.ToString());   
          } 
          finally 
          {  
              Console.WriteLine("Finally Done."); 
              reader.Dispose(); 
          } 
      
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    I have a client server application with multi-threading. The server side is failing with
    I have a server application and a client application. I'm using socket to communicate
    I have a multi threaded client server application which uses Sockets. When a new
    I have some trouble with threading in my application. I have a multi-threaded client/server
    I am thinking of creating a multi-platform portable C++ server-client application. Is it even
    I currently have heavily multi-threaded server application, and I'm shopping around for a good
    I'm making an application server client using tcp sockets in c# .. The application
    Hello I developed a multi-threaded TCP server application that allows 10 concurrent connections receives
    I have a multi-threaded console application acting as a server. The server spawns a
    I'm creating a Multi-Tenant application that uses separate databases for each 'client'. Is it

    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.