title: c# Minimalistic Telnet how to receive long string response from the server
body: <p>I use this Telnet Library: <a href="http://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library" rel="nofollow">http://www.codeproject.com/Articles/19071/Quick-tool-A-minimalistic-Telnet-library</a></p>
  
  <p>I converted the c# console program from that link into C# Winform program. I am able to send data to my device (microcontroller, to be specific) and I'm actually done in this part. The problem is that how am I suppose to read the device/server's response?</p>
  
  <p>If you're gonna open the link and read some part of the code how to use the library, it shows here how to send data to the server and read a data from the server.</p>
  
  <pre><code>// while connected
  while (tc.IsConnected &amp;&amp; prompt.Trim() != "exit" )
  {
  // display server output
  Console.Write(tc.Read());
  
  // send client input to server
  prompt = Console.ReadLine();
  tc.WriteLine(prompt);
  
  // display server output
  Console.Write(tc.Read());
  }
  </code></pre>
  
  <p>I use WriteLine in sending data to the device, as I've said, I'm done with this. I just don't know where to put the tc.Read(); part in my winform program.</p>
  
  <p>The device is send this long string with a length of 89 continuously and quite fast!</p>
  
  <p><strong><em>sensor1: 0.10meters sensor2: 1.51meters sensor3: 2.03meters sensor4: 1.7meters temp: 21degC</em></strong></p>
  
  <p>By the way, I will put each values to the specific labels. So here's my code in getting the values using substring:</p>
  
  <pre><code>    private void timer2_Tick(object sender, EventArgs e)
      {
          //"Sensor1: 0.00meters Sensor2: 0.00meters Sensor3: 0.00meters Sensor4: 0.00meters 21.00degC"
          if (connectedChecker1 == true)
          {
              dataReceived = tc.Read();
              if (dataReceived.Length == 89)
              {
                  tempVal.Text = (str1.Substring(80, 5)) + "°C";
                  frontVal.Text = str1.Substring(9, 10);
                  leftVal.Text = str1.Substring(29, 10);
                  backVal.Text = str1.Substring(49, 10);
                  rightVal.Text = str1.Substring(69, 10);
                  dataReceived = "";
              }
          }
      }
  </code></pre>
  
  <p>lol, forgive me, I really don't know where to put this part. I'm counting the length of the received data but what if there is this delay happen during the communication and the C# program reads the response as:</p>
  
  <p><strong><em>gCsensor1: 0.10meters sensor2: 1.51meters sensor3: 2.03meters sensor4: 1.7meters temp: 21deg</em></strong></p>
  
  <p>I'm now thinking to put a while loop in a button used for connecting:</p>
  
  <pre><code>    private void vesselBoardToolStripMenuItem_Click(object sender, EventArgs e)
      {
          //MessageBox.Show(ethernetIP.ToString());
          //MessageBox.Show(servoUnoIp.ToString());
          try
          {
              //create a new telnet connection for MEGA
             tc = new TelnetConnection(ethernetIP, 23);
              if (tc.IsConnected)
                connectedChecker1 = true;
  
              //create a new telnet connection for Uno
             unoTc = new TelnetConnection(servoUnoIp, 23);
             if (unoTc.IsConnected)
               connectedChecker = true;
  
             statusLbl.Text = "Connected";
  
  //INSERT WHILE LOOP HERE FOR RECEIVING DATA....
  
          }
          catch (Exception ex)
          {
              MessageBox.Show("Check the ip address of the boards");
          }
      }
  </code></pre>