Server-authoritative multiplayer netcode in C#: client-side prediction, server reconciliation and snapshot interpolation, tested under injected latency and packet loss.
Naive multiplayer sends the local position and renders whatever arrived last. With real internet latency the local player feels their own character lag behind their input, and remote players teleport between updates. The standard fix is to predict locally, let the server stay the single source of truth, and reconcile the two: the local player feels instant, the remote player looks smooth, and errors correct themselves instead of accumulating.
netcode-arena is a 2-player top-down arena (movement plus one hitscan weapon) that implements that loop end to end, with a headless UDP dedicated server and an offline test harness that simulates a lossy network deterministically.
- Client-side prediction: the local player moves on input, before any server round trip.
- Server reconciliation: on each authoritative snapshot the client snaps to the server state and replays the inputs the server has not yet acknowledged.
- Snapshot interpolation: the remote player is rendered 100 ms in the past by interpolating between two buffered snapshots, and holds its last position rather than extrapolating when the buffer runs dry.
- Shared deterministic simulation: the same
ArenaSimulation.Stepruns in the client predictor and the server tick. - Server-only damage: health changes only when an authoritative snapshot says so; the client predicts movement and weapon cooldown, never hits.
- Headless UDP server: fixed 30 tick/s loop, 20 snapshot/s broadcast, no game engine required.
- Deterministic network simulator:
SimulatedNetworkChannel<T>injects seeded latency, jitter and packet loss.
sequenceDiagram
participant P as Player input
participant C as Client<br/>ClientPredictor
participant N as Network<br/>latency and loss
participant S as Server<br/>ServerWorld
participant R as Remote render<br/>SnapshotInterpolationBuffer
P->>C: move, aim, fire (tick T)
C->>C: ApplyInput: predict locally and render now
C->>N: PlayerInput(seq, T)
N-->>S: deliver after simulated latency or drop
S->>S: Tick: authoritative ArenaSimulation.Step
S->>N: WorldSnapshot(tick, A, B)
N-->>C: deliver snapshot
C->>C: Reconcile: snap to server state,<br/>replay un-acked inputs
N-->>R: deliver snapshot (remote player)
R->>R: buffer, sample at now minus interpolation delay
NetcodeArena.Core is pure C# with no engine or socket dependencies. Because the client and the server run the identical ArenaSimulation.Step on the same (state, input), reconciliation converges: a prediction error can only come from inputs the client has not yet had acknowledged. The local player runs the predict, send, reconcile loop; the remote player is fed from the same snapshots through the interpolation buffer.
Prerequisite: .NET 7 SDK or newer. No Docker, game engine or network access is needed to build or test.
git clone https://github.com/SlateGitOrg/netcode-arena.git
cd netcode-arena
dotnet build NetcodeArena.sln -c Release
dotnet test tests/NetcodeArena.Core.Tests -c ReleaseRun the dedicated server (UDP port 7777 by default; pass another port as the argument):
dotnet run --project src/NetcodeArena.Server -c Release -- 7777Or in Docker:
docker build -t netcode-arena-server .
docker run --rm -p 7777:7777/udp netcode-arena-serverServer output on startup, with no clients connected yet:
[netcode-arena] headless server starting on udp/7777 (30 tick/s, 20 snapshot/s)
[netcode-arena] tick=150 uptime=00:04 seats=0/2 A.hp=100 B.hp=100
The first two distinct UDP endpoints that send a packet take seats A and B. Packets use the layout in WireCodec.
Using the core library directly:
var predictor = new ClientPredictor(initialState);
// each client tick: predict now and send the input to the server
uint clientTick = 42;
var move = new Vector2F(1f, 0f);
var aimDirection = new Vector2F(1f, 0f);
PlayerInput input = predictor.ApplyInput(clientTick, move, aimDirection, fire: true);
// when an authoritative state for this player arrives
bool mispredicted = predictor.Reconcile(authoritativeState);- Prediction.
ClientPredictor.ApplyInputsteps the local state and keeps the input in a pending list, tagged with a sequence number. - Reconciliation.
Reconciletakes the server's state, drops pending inputs up to the last sequence the server processed (Steptracks it per player), then replays the remainder on top.MispredictionCountandReconciliationCountexpose how often the correction was non-trivial. - Interpolation.
SnapshotInterpolationBufferkeeps snapshots sorted by tick, ignores duplicates, tolerates out-of-order arrival, and lerps between the two snapshots that bracketnow - InterpolationDelaySeconds. - Starvation. If a player's input queue is empty on a server tick, the server holds that player in place rather than guessing a movement.
- Weapon. A hitscan blaster with 14 unit range, an aim cone, a 0.35 s cooldown and 18 damage against 100 health. All constants live in
SimulationConfig, the single source of truth shared by client and server.
dotnet test tests/NetcodeArena.Core.Tests -c Release42 xUnit tests, fully offline (seeded RNG, no wall-clock waits, no sockets):
ReconciliationUnderNetworkConditionsTestsdrives a full predict, send, reconcile loop throughSimulatedNetworkChannel<T>at several loss and latency settings (up to 30% loss and 3 to 12 ticks of latency) and asserts the client converges to the server position once the network drains.SnapshotInterpolationBufferTestscover interpolation, the configured delay, out-of-order and duplicate snapshots, and holding on underrun.ClientPredictorTests,ArenaSimulationTests,WeaponSystemTests,PlayerMovementTestsandSimulatedNetworkChannelTestscover the pieces individually, including determinism.
src/NetcodeArena.Core/ simulation, prediction, reconciliation, interpolation,
simulated network channel
src/NetcodeArena.Server/ headless UDP host (HeadlessHost, WireCodec, Program)
tests/NetcodeArena.Core.Tests/ xUnit suite and network harness
Dockerfile headless server image
- No game client or engine front end is included;
NetcodeArena.Coreis designed so one can be a thin layer overClientPredictorandSnapshotInterpolationBuffer. - No lag-compensated hit detection: hits are resolved against the target's current server position.
- No matchmaking: one match per server process, seats go to the first two UDP endpoints seen.
- The UDP wire format has no authentication, encryption or reliability layer.
MIT — see LICENSE.