I am currently writing a 2D top-down shooting multiplayer game using Java. I have a game loop at the client-side that does the following in order in each game tick (The client is running in a tick rate of 60 t/s):

    1. Send an input sample (an object storing info about what keys are pressed)
       to the server through a DatagramSocket
    2. Receive info on change in game state from the server
    3. Update the client-side game state through the received info

For the server-side, I have a game loop that does the following in order in each game tick (The server is running in a tick rate of 60 t/s):

server_gameloop {
    1. Read an input sample through a single DatagramSocket
    2. Using the input sample to apply physics to the game world and update the game state

When I try to connect the first client to the server, the game flows fluently with unnoticeable delay. However, when I try to connect the second client, both first and second client start to lag behind the server greatly (delay ranges from 5s to 10s). I am currently stuck on what causes the delay. Am I handling the sending and receiving of info between client and server in a wrong way that causes game state in the server-side not being able to synchronize with those of clients'?


2 Answers 2


That is really big difference between one and two clients. The description of the server loop seem like you receive exactly one message each tick (at 60Hz). So when there are two clients sending messages, you would receive only from one client and then from the other client next tick, then from first one again, etc. So the queue of waiting messages would grow and the system would eventually start dropping messages when the receive queue is too long. That length could correspond to the 5-10s lag.

Make sure you receive all pending messages before going to wait for next week tick.

  • \$\begingroup\$ Tried reading more than one input sample within each tick and it works, thanks. \$\endgroup\$
    – ccy1997
    Commented Mar 2, 2018 at 19:13

I'm by no means a networking or Java expert but I'll give it a shot.

Does the issue get worse the more clients you add? If so, I ran into a similar issue using node and websockets (I don't have any java experience).

For me, the problem was that I was simply sending too many packets to the server. On localhost it would work fine for up to two clients, but more, or a remote server, made everything very laggy.

I fixed my issue by only sending a client's input state to the server if the input actually changed. The server can just assume a client is still doing the same if it was not notified of any changes.

Server-Side it was also key to notify the clients only of the info they absolutely needed to reconstruct any changes that happened. Using interpolation for the other players was also a must (as 60t/s was neither feasible nor required for my scenario), introducing a frame or two of delay, but greatly reducing server-load.

So, to summarize:

  • Try to only send updates to the server on input changes
  • If required, try lowering the rate at which you update clients about other clients, but keep reading a client's input changes at 60t/s.
  • Use inter/extrapolation to try and cover up the "holes" of data about the other players.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .