title: How to use TcpClient in a way that accounts for erratic stream response
body: <p>My C# skills are still rather basic, but through trial-and-error I mananged to create a Windows Forms server that allows multiple connections and a client that sends messages and gets responses.
  However, I know from distant VB experience that requests may not always match answers 1:1 as the connection can be erratic.
  My current code &quot;works&quot;, but probably it's not good. The client cannot deal with a fragmented answer from the server, and if it receives no answer, it will hang.</p>
  <p>Could my code be proofed to take this into account? Sorry if I'm using things all backwards, appreciate it if someone can bear with me!</p>
  <p>Server code:</p>
  <pre><code>        public List&lt;TcpClient&gt; tcpClient = new List&lt;TcpClient&gt;();
  
          public async void ListenForConnection(int index)
          {
              TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 5036);
              listener.Start();
              while(true)
                  try
                  {
                      tcpClient.Add(await listener.AcceptTcpClientAsync());
                      WaitForMessage(tcpClient.Count - 1);
                  }
                  catch
                  {
                  }
          }
          
          public async void WaitForMessage(int index)
          {
              NetworkStream stream = tcpClient[index].GetStream();
              StreamWriter sw = new StreamWriter(tcpClient[index].GetStream());
              while (true)
              {
                  try
                  {
                      byte[] buffer = new byte[1024];
                      await stream.ReadAsync(buffer, 0, buffer.Length);
                      int recv = 0;
                      foreach (byte b in buffer)
                      {
                          if (b != 0)
                          {
                              recv++;
                          }
                      }
                      string request = Encoding.UTF8.GetString(buffer, 0, recv); 
                      string response = &quot;You said: &quot; + request;
                      sw.WriteLine(response);
                      sw.Flush();
  
                  }
                  catch (Exception e)
                  {
                      if (!tcpClient[index].Connected)
                      { return; }
                  }
              }
  
          }
  </code></pre>
  <p>Client code:</p>
  <pre><code>        public TcpClient tcpclient;
  
          public async void ConnectToServer()
          {
             bool connected = false;
             while(!connected)
                  try
                  {
                      tcpclient = new TcpClient();
                      await tcpclient.ConnectAsync(IPADDRESS, 5036);
                      connected = true;
                  }
                  catch
                  {
                  }
          }
  
          public string ServerData(string request)
          {
              if (!tcpclient.Connected)
                  return &quot;&quot;;
              int byteCount = Encoding.UTF8.GetByteCount(request + 1);
              byte[] sendData = Encoding.UTF8.GetBytes(request);
  
              NetworkStream networkstream = tcpclient.GetStream();
              networkstream.Write(sendData, 0, sendData.Length);
  
              StreamReader sr = new StreamReader(networkstream);
              string response = sr.ReadLine();
  
              return response;
  
          }
  </code></pre>