title: TcpClient Communication with c#
body: <p>I want to make a communication program with TcpClient. But I can't receive the string message I sent from the server in the streamreader on the client side. Could the logic of the code be wrong or what way should I follow? Thank you for your help.</p>
  <pre><code> private async Task HandleTcpClientAsync(TcpClient client)
          {
              NetworkStream networkStream = client.GetStream();
              StreamWriter streamWriter = new StreamWriter(networkStream);
  
              await streamWriter.WriteAsync(&quot;Arpa Bugday Zammaney...&quot;);
  
              await networkStream.FlushAsync();
          }
         
          [HttpGet]
          public async Task&lt;IActionResult&gt; TcpListenerEndPoint()
          {
              try
              {
              IPAddress ipAd = IPAddress.Parse(&quot;127.0.0.1&quot;);
              TcpListener listener = new TcpListener(IPAddress.Loopback, 44399);
              listener.Start();
  
              CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
  
              _ = Task.Run(async () =&gt;
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        var tcpClient = await listener.AcceptTcpClientAsync();
                        _ = this.HandleTcpClientAsync(tcpClient);
                    }
                });
              TcpClient client = new TcpClient();
              await client.ConnectAsync(&quot;localhost&quot;, 44399);
  
              NetworkStream ns = client.GetStream();
              StreamReader sr = new StreamReader(ns); 
              string message = SerialCommunication.ReadLineSingleBreak(sr);
              if(message != null)
              return Ok(new GeneralResponse&lt;string&gt;
              {
                  IsError = false,
                  Result = message
              });
              return Ok(new GeneralResponse
              {
                  ErrorMessage = &quot;Herhangi Bir Veri Alınamadı.&quot;
              }); 
              }catch(Exception ex)
              {
                  return Ok(new GeneralResponse
                  {
                      ErrorMessage = ex.Message
                  });
              }
  
  </code></pre>
  <p>Here is the code where I read character by character from streamreader.</p>
  <pre><code> public static string ReadLineSingleBreak(this StreamReader self)
          {
              StringBuilder currentLine = new StringBuilder();
              int i;
              char c;
              while ((i = self.Read()) &gt;= 0)
              {
                  c = (char)i;
                  if (c == '\r'
                      || c == '\n')
                  {
                      break;
                  }
  
                  currentLine.Append(c);
              }
  
              return currentLine.ToString();
          }
  </code></pre>