Okay, so I've got a TCP Asynchronous Multithreaded server running which interacts with multiple clients at the same time.
It works fine unless a client sends many messages in a short peroid of time, for example five messages within about half a second. For some reason it only recieves the first four of these and ignores the fifth. I've debugged the application and it seems it doesn't even see a packet coming in when it ignores them, but I know it's being sent by the client because I've packet logged it via WPE Pro.
My code for receiving is:
private void Run()
{
try
{
if (ClientCallback != null && ClientSocket.Connected)
{
Output.Write("Server", false, "Receiving packet...");
ClientSocket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ClientCallback, null);
}
else
{
Disconnect(1000);
}
}
catch (Exception e)
{
Output.Write("Client " + ClientID, true, e.ToString());
Disconnect(1000);
}
}
And the code for finishing the receive is:
private void Receive(IAsyncResult result)
{
try
{
Output.Write("Server", false, "Received packet.");
// Finish receiving the data
int PacketLength = ClientSocket.EndReceive(result);
RecvPacket.SetPacket(System.Text.Encoding.Default.GetString(Buffer, 0, PacketLength));
Output.Write("Server", false, "Packet: " + RecvPacket.GetPacket());
// Check packet length
if (RecvPacket.GetLength() <= 0)
{
// Client disconnected
Disconnect(1000);
}
else
{
// Parse the packet
PacketParser.ParsePacket(RecvPacket);
// Continue running
Run();
}
}
catch (Exception e)
{
if (!(e is System.Net.Sockets.SocketException))
{
Output.Write("Client " + ClientID, true, e.ToString());
}
Disconnect(1000);
}
}
This all works fine as I said if the client is sending only about one or two messages per second, but sending a lot of messages works only about 90% of the time. Since I know these messages are being sent I'm very confused on how to fix this problem.
Any ideas?