Async Serveur TCP pour plusieurs Clients

J'ai un Serveur TCP qui est à l'écoute de façon asynchrone pour les connexions entrantes. Tout fonctionne bien si un client est connecté. Mais si il y a deux ou plus de connexions, le Serveur ne reçoit pas le premier message. Quand je debug le ReceiveCallback fonction, je peux voir que le Serveur renvoie la longueur du message mais pas les données. I. e. si je connecte deux clients et essayez d'envoyer le premier message: "bonjour", le serveur obtient: = 5; tampon= /0/0/0/0/0, donc, rien n'est affiché. Dans le second message de la même client, le serveur reçoit les données.

c'est comment mon serveur ressemble:

        private void StartServer()
{
try
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 3333));
serverSocket.Listen(100);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);   
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AcceptCallback(IAsyncResult ar)
{
try
{
Socket clientSocket = serverSocket.EndAccept(ar); 
clientSocketList.Add(clientSocket);
AppendToTextBox("ClientConnected");
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), clientSocket);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
int received = 0;
Socket current = (Socket)ar.AsyncState;
received = current.EndReceive(ar);
byte[] data = new byte[received];
if (received == 0)
{
return;
}
Array.Copy(buffer, data, received);
string text = Encoding.ASCII.GetString(data);
AppendToTextBox(text);
buffer = null;
Array.Resize(ref buffer, current.ReceiveBufferSize);
current.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

OriginalL'auteur xvzwx | 2013-04-03