I have created a server that works perfectly. However when I decided to test it against a DDoS attack I found a major memory leak. The code below, will fill up RAM (when DDoS'd) and eventually crash my computer. I have tried to implement counter-measures however the memory leak still exists. My current code for listening is:
TcpClient connection = server.AcceptTcpClient();
if (!IpByConnection.ContainsKey(connection.Client.RemoteEndPoint.ToString().Split(':')[0])) {
IpByConnection.Add(connection.Client.RemoteEndPoint.ToString().Split(':')[0], connection);
CipherNetwork network = new CipherNetwork(connection);
network.ListenOnDifferentThread(true);
network.DisconnectOnError(true);
network.StartListening();
TotalConnections += 1;
} else {
connection.Close();
}
How would I fix this memory leak ?
EDIT: I forgot to mention, the method is run on a separate thread.
EDIT 2: I have tried setting it to null, disposing it and both.
I was unable to find a direct solution however I managed to solve the problem by limiting each connection 5 per IP, using A Dictionary to store the connected users and remove them once they disconnected.