DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2005

    Closing network streams


    I have been writing a simple tcp client and server. I want to verify when to close the network stream. I have a the server thread pushing out data to the listener, but it does so as new data gets generated. Do I close the network stream when I'm done sending a particular unit of data? Or do I only close the stream when I'm totally done with the client and want to disconnect?

    m_tcOutput is already a connected tcpClient.

    System.Net.Sockets.NetworkStream	nsOut = null;
    	nsOut = m_tcOutput.GetStream();
    	nsOut.Write(data, 0, data.Length);
    is the above ok? Or should I remove the nsOut.Close() call? Remember that I am not done yet, and I will signal the thread to send the client some more data later.

  2. #2
    Join Date
    Aug 2005

    Got the answer.

    Through trial and error I found that you should leave the stream open and just forget about it. The tcpClient object will do almost all the cleanups necessary, and calling GetStream() a billion times will only return a billion references to the stream object. It is kind of funny though that you still have to get the stream and close it separate of the tcpClient.close() call. I wish that the tcpClient object had a property that indicated the connection state though, or that I could append an onClose event.

    While I appreciate the ability of the NetworkStream objects I guess I'm not too partial to the AsyncCallback system using BeginRead.

  3. #3
    Join Date
    Aug 2005

    Lies!! All of it!!

    Quote Originally Posted by MSDN
    The Close method closes the TCP connection. It calls the Dispose method passing a true value to release all managed and unmanaged resources associated with the TcpClient. These resources include the underlying Socket used for connecting with the remote host, and the NetworkStream used to send and receive data.
    But in their example they say
    tcpClient.Close ();

    // Closing the tcpClient instance does not close the network stream.
    netStream.Close ();

  4. #4
    Join Date
    Nov 2004
    And for your information, this is what the documentation says about NetworkStream.Flush()

    Flushes data from the stream. This method is reserved for future use.

Similar Threads

  1. Replies: 0
    Last Post: 03-29-2001, 05:39 PM
  2. Network Names
    By Alan Gillott in forum VB Classic
    Replies: 0
    Last Post: 03-09-2001, 01:42 PM
  3. Network Names
    By Alan Gillott in forum VB Classic
    Replies: 0
    Last Post: 03-09-2001, 01:42 PM
  4. Evaluating Digital Dashboard for NT network.
    By aoshell in forum Enterprise
    Replies: 0
    Last Post: 10-13-2000, 09:51 AM
  5. Replies: 0
    Last Post: 05-19-2000, 01:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.