title: C# Why doesn't "Flush" force the bytes down the network stream?
body: <p>I have a project where I'm trying to send a serialized object to the server, then wait for an &quot;OK&quot; or &quot;ERROR&quot; message to come back.</p>
  <p>I seem to be having a similar problem to th poster of : <a href="https://stackoverflow.com/questions/4898356/tcpclient-send-close-problem">TcpClient send/close problem</a></p>
  <p>The issue is that the only way I seem to be able to send the original object is to close the connection, but then (of course) I can't wait to see if the object was processed successfully by the server.</p>
  <pre><code>private void button4_Click(object sender, EventArgs e)
  {
      RequestPacket req = new RequestPacket();
  
      /// ... Fill out request packet ...
  
      /// Connect to the SERVER to send the message...
      TcpClient Client = new TcpClient(&quot;localhost&quot;, 10287);
      using (NetworkStream ns = Client.GetStream())
      {
          XmlSerializer xml = new XmlSerializer(typeof(RequestPacket));
          xml.Serialize(ns, req);
  
          /// NOTE: This doesn't seem to do anything.... 
          ///       The server doesn't get the object I just serialized.
          ///       However, if I use ns.Close() it does... 
          ///          but then I can't get the response.
          ns.Flush();
  
          // Get the response. It should be &quot;OK&quot;.
          ResponsePacket resp;
  
          XmlSerializer xml2 = new XmlSerializer(typeof(ResponsePacket));
          resp = (ResponsePacket)xml2.Deserialize(ns);
  
  
          /// ... EVALUATE RESPONSE ...
      }
  
      Client.Close()
  }
  </code></pre>
  <p>UPDATE: In response to one commenter, I don't think the client can be at fault. It is simply waiting for the object, and the object never comes until I close the socket.... however, if I'm wrong, I'll GLADLY eat crow publicly. =) Here's the client:</p>
  <pre><code>    static void Main(string[] args)
      {
          // Read the port from the command line, use 10287 for default
          CMD cmd = new CMD(args);
          int port = 10287;
  
          if (cmd.ContainsKey(&quot;p&quot;)) port = Convert.ToInt32(cmd[&quot;p&quot;]);
  
          TcpListener l = new TcpListener(port);
          l.Start();
  
          while (true)
          {
              // Wait for a socket connection.
              TcpClient c = l.AcceptTcpClient();
              
              Thread T = new Thread(ProcessSocket);
  
              T.Start(c);
          }
      }
  
  
      static void ProcessSocket(object c)
      {
          TcpClient C = (TcpClient)c;
  
          try
          {
              RequestPacket rp;
              //// Handle the request here.
              using (NetworkStream ns = C.GetStream())
              {
                  XmlSerializer xml = new XmlSerializer(typeof(RequestPacket));
                  rp = (RequestPacket)xml.Deserialize(ns);
              }
  
              ProcessPacket(rp);
          }
          catch
          {
              // not much to do except ignore it and go on.
          }
      }
  </code></pre>
  <p>Yeah.... it's that simple.</p>