Search code examples
c#multithreadingudpudpclientmmo

C# MMO server structure


I am at the beginning of writing an simple mmo server for demo game. I am using UDP protocol (UdpClient) but I wonder, how other servers are working?:

  1. only 1 UDP client that process all connected clients and messages and responds to them
  2. server creates a new UDP client instance for each connected player?

I have tested connecting 50 clients who send "Ping" message and server has to reply with "Pong". It takes about 10 seconds (on localhost) for the 50th "Pong" to send (in the mean time, server is supposed to send ping to 1st player as well so queue can be prolonged to hours which is absurd)


Solution

  • Other servers use 1) i.e. listen from any client with one Socket (of which I assume there is one per UdpClient) . Furthermore:

    • This one Socket should be used for the lifetime of the server, i.e. don't create a new one when done with one datagram.
    • Receives and sends should be done asynchronously so receiving/sending one stream doesn't hold up receiving/sending another stream.
    • Beware of the amount of data you send/receive - if you send/receive data to several clients from the server the total amount of data is multiplied by the amount of clients.

    Doing the above on localhost with 50 clients sending ping and receiving a pong that is only a few bytes should take less than 1ms, assuming some other process is not maxing out the processors.