title: C# Socket.Receive message length
body: <p>I'm currently in the process of developing a C# Socket server that can accept multiple connections from multiple client computers. The objective of the server is to allow clients to "subscribe" and "un-subscribe" from server events.</p>
  
  <p>So far I've taken a jolly good look over here: <a href="http://msdn.microsoft.com/en-us/library/5w7b7x5f(v=VS.100).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/5w7b7x5f(v=VS.100).aspx</a> and <a href="http://msdn.microsoft.com/en-us/library/fx6588te.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/fx6588te.aspx</a> for ideas.</p>
  
  <p>All the messages I send are encrypted, so I take the string message that I wish to send, convert it into a byte[] array and then encrypt the data before pre-pending the message length to the data and sending it out over the connection.</p>
  
  <p>One thing that strikes me as an issue is this: on the receiving end it seems possible that Socket.EndReceive() (or the associated callback) could return when only half of the message has been received. Is there an easy way to ensure each message is received "complete" and only one message at a time?</p>
  
  <p><strong>EDIT:</strong> For example, I take it .NET / Windows sockets does not "wrap" the messages to ensure that a single message sent with Socket.Send() is received in one Socket.Receive() call? Or does it?  </p>
  
  <p>My implementation so far:</p>
  
  <pre><code>private void StartListening()
  {
      IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
      IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0], Constants.PortNumber);
  
      Socket listener = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
      listener.Bind(localEP);
      listener.Listen(10);
  
      while (true)
      {
          // Reset the event.
          this.listenAllDone.Reset();
  
          // Begin waiting for a connection
          listener.BeginAccept(new AsyncCallback(this.AcceptCallback), listener);
  
          // Wait for the event.
          this.listenAllDone.WaitOne();
      }
  }
  
  private void AcceptCallback(IAsyncResult ar)
  {
      // Get the socket that handles the client request.
      Socket listener = (Socket) ar.AsyncState;
      Socket handler = listener.EndAccept(ar);
  
      // Signal the main thread to continue.
      this.listenAllDone.Set();
  
      // Accept the incoming connection and save a reference to the new Socket in the client data.
      CClient client = new CClient();
      client.Socket = handler;
  
      lock (this.clientList)
      {
          this.clientList.Add(client);
      }
  
      while (true)
      {
          this.readAllDone.Reset();
  
          // Begin waiting on data from the client.
          handler.BeginReceive(client.DataBuffer, 0, client.DataBuffer.Length, 0, new AsyncCallback(this.ReadCallback), client);
  
          this.readAllDone.WaitOne();
      }
  }
  
  private void ReadCallback(IAsyncResult asyn)
  {
      CClient theClient = (CClient)asyn.AsyncState;
  
      // End the receive and get the number of bytes read.
      int iRx = theClient.Socket.EndReceive(asyn);
      if (iRx != 0)
      {
          // Data was read from the socket.
          // So save the data 
          byte[] recievedMsg = new byte[iRx];
          Array.Copy(theClient.DataBuffer, recievedMsg, iRx);
  
          this.readAllDone.Set();
  
          // Decode the message recieved and act accordingly.
          theClient.DecodeAndProcessMessage(recievedMsg);
  
          // Go back to waiting for data.
          this.WaitForData(theClient);
      }         
  }
  </code></pre>