title: C# - Some packets are dropped while reading from the TCP connection
body: <p>I am using C# to read continues stream of data(ITCH Data i.e Forex Prices) over a TCP connection, but after running the application for longer durations sometimes the application drops a packet and the information is lost.</p>
  
  <p>Below is the code snippet which i am using to read the data:</p>
  
  <pre><code>private void ReaderThreadStarter()
      {
          StreamReader streamReader = new StreamReader(this._networkStream);
          while (!_stopping)
          {
              try
              {
                  if (this._networkStream.DataAvailable)
                  {
                      while ((line = streamReader.ReadLine()) != null)
                      {
                          lock (_queue.ConcurrentQueue)
                          {
                              byte[] data = System.Text.Encoding.ASCII.GetBytes(line);
                              Log.Info("Data Added in Queue: " + Encoding.ASCII.GetString(data, 0, data.Length));
                              _queue.WriteToQueue(data);
                          }
                      }
                  }
              }
              catch (Exception exception)
              {
                  Log.Error(exception);
              }
              finally
              {
                  SetStopped();
              }
          }
      }
  </code></pre>
  
  <p>What the above piece of code does is that it reads the data from the TCP connection and write it into a concurrent queue, and another thread then uses the data in the queue for processing. So basically a simply producer-consumer problem.</p>
  
  <p>The producer-consumer part seems to work fine as what I write in the queue gets used by the consumer.</p>
  
  <p>One option was to use the sniffer and confirm that the application was dropping packets but I am working in an environment where i can't use the sniffers. The reason I believe that there are packet drops is because for some of my forex orders I never get cancellations and my prices go off and the data providers tell me that the pricing is correct at there end.</p>
  
  <p>I am also logging the data which I read from the TCP port before saving in the queue, so from the logs I assume that the data gets lost in reading from the connection.</p>
  
  <p>Can someone tell me what I might be doing wrong here or what could be the reasons for dropping the packets.</p>
  
  <p>Below is the code snippet of my consumer code:</p>
  
  <pre><code>public void ReadQueue()
      {
          try
          {
              while (true)
              {
                  {
                      byte[] data = _queue.ReadFromQueue();
  
                      Parse(data);
                  }
              }
          }
          catch (Exception exception)
          {
              Log.Error(exception);
          }
      }
  
  public byte[] ReadFromQueue()
      {
          try
          {
              byte[] data;
              lock (this) // Enter synchronization block
              {
                  ConcurrentQueue.TryDequeue(out data);
              } 
              return data;
          }
          catch (Exception exception)
          {
              Log.Error(exception);
              return null;
          }
      }
  </code></pre>