title: C# Async Sockets Server Receive Problems
body: <p>I have implemented my Server application regarding this post here: <a href="http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c8781#Client1" rel="nofollow">http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c8781#Client1</a></p>
  
  <p>Sum up: I am using async Sockets ala BeginAccept(..), BeginReceive(..).
  My Server is capable of handling mutliple clients and everything works fine until a client performas two or more synchronous send operation without waiting some time. The client does not get any error and so is not notified, that the server does not get the second message! If the client waits approx. 100ms after the first send operation, everything works fine.
  I thought that when i use TCP i can ensure that the server receives the message. (Except there is an exception thrown)! 
  Could you provide me a solution to fix this. </p>
  
  <p>Here are the WaitForData(..) &amp; OnDataReceive(..) Methods that i implemented in the server</p>
  
  <pre><code>public void WaitForData(MyClient client)
  {
      try
      {
          if (pfnCallBack == null)
          {
              pfnCallBack = new AsyncCallback(OnDataReceived);
          }
  
          iarResult = client.Socket.BeginReceive(client.DataBuffer,
                                                  0, client.DataBuffer.Length,
                                                  SocketFlags.None,
                                                  pfnCallBack,
                                                  client);
      }
      catch (SocketException se)
      {
          MessageBox.Show("SocketException@WaitForData" + se.Message);
      }
  }
  public void OnDataReceived(IAsyncResult asyn)
  {
      try
      {
          MyClient user= (MyClient)asyn.AsyncState;
          int iRx = user.Socket.EndReceive(asyn);
  
          byte[] receivedData = user.DataBuffer;
  
          MemoryStream memStream = new MemoryStream();
          BinaryFormatter binForm = new BinaryFormatter();
          memStream.Write(receivedData, 0, receivedData.Length);
          memStream.Seek(0, SeekOrigin.Begin);
          MyMessage msg = (MyMessage)binForm.Deserialize(memStream);
  
          switch (msg.Command)
          {
              case (MyMessage.MyCommand.ConnId):
                  this.connId = (int) msg.MyObject;
                  tsslConnStatus.Text += " | ID: " + connId.ToString();
              break;
  
              case (MyMessage.MyCommand.Text):
                  MessageBox.Show(msg.MyObject.ToString());
                  break;
          }
          WaitForData(server);
      }
      catch (ObjectDisposedException ode)
      {
          MessageBox.Show("ObjectDisposedException@OnReceiveData" + ode.Message);
      }
      catch (SocketException se)
      {
          MessageBox.Show("SocketException@OnReceiveData" + se.Message);
      }
  }
  </code></pre>
  
  <p>The CLIENT calls a synchronous SEND METHOD TWICE or MORE! server INSTANCEOF MyClient</p>
  
  <pre><code>if (server.Socket.Connected)
  {
      BinaryFormatter bf = new BinaryFormatter();
      MemoryStream ms = new MemoryStream();
      bf.Serialize(ms, message);
      MyMessage = new MyMessage(something);
      server.Socket.Send(ms.ToArray());
  }
  </code></pre>
  
  <p>so, i think this code snippets must be enough for you to get the idea i was trying to use!
  If you need further details or code snippets, just tell me i will post it!</p>
  
  <p>Thanx!</p>