title: Constant reading from StreamReader from TCP connection, C#
body: <p>I have working script in Swift, perfectly reading TCP connection stream non-stop.
  I need to make the same in C# and here i got stuck.
  The remote service works like this: it waits for the first command, which is "*99*1##", then it replies "*#*1##" and start forwarding another similar messages to the same stream. All i need is to record all those server answers to a file.
  The problem is i can't get more then one server answer. Just after first server answer, i'm not able to read from the stream anymore. Peek()/Read() methods return -1, and ReadLine returns null. And i'm sure that it's wrong as from Swift script i can see that server sends me much more then just one message.
  First, here is Swift code (reading stream)</p>
  
  <pre><code>    let queryString: String = "*99*1##"
      let queryData: Data = queryString.data(using: String.Encoding.utf8, allowLossyConversion: false)! as Data
      queryData.withUnsafeBytes { outputStream.write($0, maxLength: queryData.count) }
      var readChars: Int = 0
      while true {
          readChars = inputStream.read(&amp;buffer, maxLength: buffer.count)
          if (readChars &gt; 0) {
              let readString: String = NSString(data: NSData(bytes:buffer, length:readChars) as Data, encoding: String.Encoding.utf8.rawValue)! as String
              print("\(readString)\n")
          } else {
              print ("Server closed connection\n")
              inputStream.close()
              outputStream.close()
              break
          }
      }
  </code></pre>
  
  <p>Communication looks like that:</p>
  
  <pre><code>Client-&gt;*99*1##
  Server-&gt;*#*1##
  Server-&gt;*1*34*#6#4#02##
  Server-&gt;*1*1*13#4#01##
  Server-&gt;*1*0*13#4#01##
  and etc...
  </code></pre>
  
  <p>In C# i have tried StreamReader methods like ReadLine, Read, ReadToEnd and none of them gave me more then one server answer.</p>
  
  <pre><code>string address = "192.168.0.10";
  int port = 20000;
  TcpClient client = new TcpClient(address, port);
  StreamReader sr = new StreamReader(client.GetStream());
  StreamWriter sw = new StreamWriter(client.GetStream());
  string command = "*99*1##";
  Console.WriteLine("Client-&gt;" + command);
  sw.WriteLine(command);
  sw.Flush();
  
  string data;
  while (true)
  {
      data = sr.ReadToEnd();
      if (data != null) 
      {
          Console.WriteLine(Server-&gt;" + data);
      } else
      {
          break;
      }
  }
  Console.WriteLine("Server closed connection.");
  </code></pre>
  
  <p>Here communication looks totally wrong:</p>
  
  <pre><code>Client-&gt;*99*1##
  Server-&gt;*#*1##
  Server closed connection
  </code></pre>
  
  <p>Second and other server answers comes after 5 to 10 seconds later then first. Maybe there is some timeout problem on my side? Server has timeout around 30 mins and i can keep session with it through the Swift or Telnet that long.</p>
  
  <p>Thank you in advance for all the ideas.</p>