title: Receiving blocks of data from a TCP IP hardware device using c#
body: <p>I have a barcode scanner hardware device that continually scans barcodes passing passing by and sends the data through it's tcpip port.  The scanner is installed on a production line and reads all passing barcodes for verification purposes.  There isn't any information sent to the barcode scanner other than what might be sent through the NetworkStream class upon connection.  I implemented a very simple interface to verify connectivity and receipt of the expected data being sent by the device. Obviously the problem with this code is it only iterates a certain number of times.  Regardless of the looping choice, I need an app that receives data and continues to receive data until my code chooses to stop close the connection and stop receipt of data.  It has to always be watching the port for incoming data from the device's tcp ip server.</p>
  
  <pre><code>public void readData()
      {
          int n = 1;
  
              Byte[] data;
              Int32 port = 51236;
              TcpClient client = new TcpClient("192.168.0.70", port);
              NetworkStream stream = client.GetStream();
              String responseData = String.Empty;
              try
              {
                  while (n &lt; 50)
                  {
                  data = new Byte[256];  //arbitrary length for now
  
                  // String to store the response ASCII representation.
  
                  // Read the first batch of the TcpServer response bytes.
                  Int32 bytes = stream.Read(data, 0, data.Length);
                  responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                  m_upcCode = responseData;
                  n++;
                  }
              }
              catch (ArgumentNullException ArgEx)
              {
                  m_upcCode = "ArgumentNullException: {0}" + ArgEx.Message.ToString();
              }
              catch (SocketException exSocket)
              {
                  m_upcCode = "SocketException: {0}" + exSocket.Message.ToString();
              }
      }
  </code></pre>
  
  <p>I've been looking through numerous posts regarding establishing a connection to a tcp ip server and receiving data from the server.  The post I found with the most promise might be <a href="https://stackoverflow.com/questions/20694062/can-a-tcp-c-sharp-client-receive-and-send-continuously-consecutively-without-sle">Can a TCP c# client receive and send continuously/consecutively without sleep?</a>. I'm just having an issue understanding how to implement the design.  I would be happy just to see the code from that post simply called and implemented on a form and display data in a text box.  The data being received is always a fixed length of 12 digits of a bar code and a .  The data doesn't stop unless the conveyor belt stops.  But we don't want the connection to close just because of a line pause.  There was also a post at <a href="https://stackoverflow.com/questions/37620952/c-sharp-tcp-client-listening-for-data">C# TCP Client listening for data</a> specifically trying to address reading barcodes but didn't get much of a response.</p>