title: NetworkStream never writes data
body: <p>Alright, so I understand the whole idea that TCP is stream based and not message based - I don't care about that.</p>
  
  <p>What I am trying to do is I am trying to simply serialize something and send it over the network to another application that speaks the same message-based protocol.</p>
  
  <p>The problem is that whenever I serialize the data (I'm serializing to XML), and then write it to the network stream, the darn thing never ever writes it - ever. It is not until I close the program and the stream is closed that the stream actually sends the data. What the heck is happening - Is TCP waiting for enough data to send? Is it waiting for something from me? (I doubt the former because I can do simple write lines and it does those just fine.)</p>
  
  <p>Here is the code for my client:</p>
  
  <pre><code>        TcpClient client = new TcpClient();
          client.Connect(IPAddress.Parse("127.0.0.1"), 10100);
          NetworkStream ns = client.GetStream();
          StreamWriter writer = new StreamWriter(ns);
  
          ListenerThread th = new ListenerThread(new StreamReader(ns));
          new Thread(th.run).Start();
  
          XmlSerializer serializer = new XmlSerializer(typeof(Message), new Type[] {typeof(AuthenticationMessage), typeof(ChangeChatRoomMessage), typeof(ChangePasswordMessage), typeof(ConnectionStatusMessage), typeof(InitializeMessage), typeof(StatusMessage), typeof(SuccessMessage), typeof(TextMessage)});
  
          string file =  "&lt;some test xml file&gt;";
          while(true)
          {
              FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
              StreamReader reader = new StreamReader(stream);
              Message newmsg = (Message)serializer.Deserialize(reader);
              stream.Close();
  
              serializer.Serialize(writer, newmsg);
              writer.Flush();
  
              file = Console.ReadLine();
          }
  </code></pre>
  
  <p>Here is the code for the server:</p>
  
  <pre><code>    public void HandleMessage()
      {
          Message msg = (Message)serializer.Deserialize(reader);
          Console.WriteLine("Read Message " + msg.GetType());
      }
  
      public void Start()
      {
          while(true)
          {
              Socket socket = listener.AcceptSocket();
              NetworkStream stream = new NetworkStream(socket);
              reader = new StreamReader(stream);
              HandleMessage();
              stream.Close();
          }
      }
  </code></pre>
  
  <p>My program has been running for 20 minutes now, and nothing has been sent to the server. I tried everything I could think of, flush my writer buffer (The TextWriter, not the NetworkStream obviously), and set the client to NoDelay, etc... What is happening and why?</p>