title: Socket not reading all the packets that arrive
body: <p>I am working on a C# TCP client and monitoring my packets using Microsoft Network Monitor. There is a server that's basically a black box sends an N amount of packets (right now it's in the order of 10-20) with a delay of 0,1 ms to 1 ms between each of them and the next one. </p>
  
  <p>The packets that are read by my client are in order and of course most arrive in larger chunks than appear on Network Monitor since TCP receives the stream. My problem is some packets fail to arrive (I did check the previous chunk of information to make sure it's not there. They aren't stored in the wrong order either.).</p>
  
  <p>So is it possible that my client is missing some of the information somehow? Are the packets being sent too frequently? Sadly I cannot tamper with their frequency. I add here some of my code, if you could enlighten me as to why packets that arrive are not read and how to solve this I would be most grateful.</p>
  
  <p><strong>This is where I first call BeginReceive:</strong></p>
  
  <pre><code>private static void AcceptCallback(IAsyncResult result)
  {
      ConnectionInfo connection = new ConnectionInfo();
      MensajeRecibido msj = new MensajeRecibido();
      try
      {
          // Finish Accept
  
          Socket s = (Socket)result.AsyncState;
          connection.Socket = s.EndAccept(result);
          msj.workSocket = connection.Socket;
          connection.Socket.Blocking = false;
          connection.Buffer = new byte[255];
          lock (connections) connections.Add(connection);
  
          // Start Receive
  
          connection.Socket.BeginReceive(msj.buffer, 0,
          msj.buffer.Length, SocketFlags.None,
          new AsyncCallback(ReceiveCallback), msj);
          // Start new Accept
          serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
      }
      catch (SocketException exc)
      {
          //Log here
      }
      catch (Exception exc)
      {
          //Log here
      }
  }
  </code></pre>
  
  <p><strong>This is the callback:</strong></p>
  
  <pre><code>private async static void ReceiveCallback(IAsyncResult result)
  {
      MensajeRecibido mensaje = new MensajeRecibido();
      mensaje = (MensajeRecibido)result.AsyncState;
  
      try
      {
          mensaje.workSocket.EndReceive(result);
          mensaje.EstaCompleto();
          mensaje.workSocket.BeginReceive(mensaje.buffer, 0,
          mensaje.buffer.Length, SocketFlags.None,
          new AsyncCallback(ReceiveCallback), mensaje);
      }
      catch (SocketException)
      {
          //Log
      }
      catch (Exception)
      {
          //Log
      }
  }
  </code></pre>
  
  <p><strong>And this is the method <code>EstaCompleto()</code> which basically converts the message and adds it to a list.</strong> (It returns true or false because it's actually meant to go in an if clause but until I get rid of this problem that really serves no purpose)</p>
  
  <pre><code>public bool EstaCompleto()
  {
      MensajeActual = Destuffing(ByteToFrame_Decoder(buffer)); //This translates the message to an understandable string
      Mensajes.Enqueue(MensajeActual);
      if(MensajeActual.Contains("&lt;ETX&gt;"))
      {
          return true;
      }
      else return false;
  }
  </code></pre>
  
  <p><strong>Edit 25/3/15:</strong>
  <strong>Here's the rest of the class MensajeRecibido.</strong></p>
  
  <pre><code> public class MensajeRecibido
  {
  
      public Socket workSocket = null;
      // Size of receive buffer.
      public const int BufferSize = 25500; 
      // Receive buffer.
      public byte[] buffer = new byte[BufferSize];
      public string UltimoMensajeLeido = "0";
      public string MensajeActual = "0";
      public Queue&lt;string&gt; Mensajes = new Queue&lt;string&gt;();
      public IPAddress IPRack;
  
  
      //*******************************************************************
  
      public bool EstaCompleto()
      ///See code in the previous sample
  
  
      //*******************************************************************
      public string ByteToFrame_Decoder(byte[] frame)
      {
          string answer = null;
          UTF8Encoding ObjDecoder = new System.Text.UTF8Encoding();
          char[] array_chares = new char[frame.Length];
          string msj_neg = null;
          string titlemsg = "Atención";
          try
          {
              int cant = ObjDecoder.GetChars(frame, 0, frame.Length, array_chares, 0);
          }
          catch (EncoderFallbackException EncFbackEx)
          {
              msj_neg = "No hay comunicación";
              //   System.Windows.Forms.MessageBox.Show(msj_neg, titlemsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
          }
          answer = decode_string(array_chares);
          return answer;
      } // fin método ByteToFrame_Decoder()
  
      //*******************************************************************
      public string Destuffing(string msjstuff)
      {
          string destuffed = null;
          string matched = null;
          string original = null;
          int largo = msjstuff.Length;
  
          for (int i = 0; i &lt; (largo - 1); i++)
          {
              matched = msjstuff.Substring(i, 2);
              original = msjstuff.Substring(i, 1);
              if (original != " ")
              {
                  switch (matched)
                  {
                      case "EX":
                          { original = "D"; i++; } break;
  
                      case "ex":
                          { original = "d"; i++; } break;
  
                      case "EE":
                          { original = "E"; i++; } break;
  
                      case "ee":
                          { original = "e"; i++; } break;
  
                      case "  ":
                          { original = ""; i += 2; } break;
                  }
                  destuffed = destuffed + original;
              }
              else
              {
                  i++;
              }
  
          }
          destuffed = destuffed + "&gt;";
          return destuffed;
  
      } //fin método Destuffing()
      //*******************************************************************
      public static string decode_string(char[] ArrChar)
      {
          string text = null;
          string reply = null;
          foreach (char letter in ArrChar)
          {
              int value = Convert.ToInt32(letter);
              string hexOutput = String.Format("{0:X}", value); // Convert the decimal value to a hexadecimal value in string form.
              switch (hexOutput)
              {
                  case "20":
                      text = " ";
                      break;
                  case "1":
                      text = "&lt;SOH&gt;";
                      break;
                  case "2":
                      text = "&lt;STX&gt;";
                      break;
                  case "3":
                      text = "&lt;ETX&gt;";
                      break;
                  case "4":
                      text = "&lt;EOT&gt;";
                      reply = reply + text;
                      goto Finish;
                  case "5":
                      text = "&lt;ENQ&gt;";
                      break;
                  case "6":
                      text = "&lt;ACK&gt;";
                      break;
                  case "15":
                      text = "&lt;NAK&gt;";
                      break;
                  case "17":
                      text = "&lt;ETB&gt;";
                      break;
                  case "1E":
                      text = "&lt;RS&gt;";
                      break;
                  /*case "23":
                      text = "#";
                      break;
                  case "24":
                      text = "$";
                      break;
                  case "26":
                      text = "&amp;";
                      break;*/
                  default:
                      text = letter.ToString();
                      break;
              }
              reply = reply + text;
          }
      Finish: ; //salimos del foreach
          return reply;
      } //fin método decode_string()
      //*******************************************************************
  
  }
  </code></pre>