-1

I have a C# server application, which provides services based on packets. For example: The client connects, sends a packet named "do X action", and the server elaborates such packet.

The problem is, people can use WPE or packet managers to flood requests to the server and send up to 1000 packets per second, obviously causing the server to crash.

Could someone give me an idea, on how actually I can keep a packet count, maybe based on IP or sessions? For example: more than 30 packets per second, the server disconnects the client.

Is this possible?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Kasai
  • 1
  • 2
  • The answer is "yes", but as it stands, this question is far too broad to answer concisely. Assuming you have some code already written that implements packets, you should consider posting some of it so we can help you more precisely. – aevitas Apr 24 '17 at 20:58

2 Answers2

0

You will have 1 TcpClient per connection on the server side. If you call Read() on the underlying NetworkStream of the TcpClient and you get lots of kilobytes (TCP will not retain the packet structure, only UDP will), you can imagine that there's something wrong, then disconnect and remember the IP to filter further incoming connection requests.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
0

How about at the point where you have decoded the stream into commands, you just stop processing them when you reach a certain amount in progress:

static int commandsInProcessing = 0;
void CommandReceived()
{
   while(commandsInProcessing >= 30)
   { 
      Sleep(100);
   }

   commandsInProcessing++;
   ProcessCommand();
   commandsInProcessing--;
}

Obviously, you will need to run CommandReceived within threads, and it should use Semaphores not ints, but hopefully this will give you the general picture.

Neil
  • 11,059
  • 3
  • 31
  • 56