Idea for online multiplayer turn-based game with UDP

03-02-2013, 08:25 AM
Idea for online multiplayer turn-based game with UDP


Online multiplayer turn-based game mentioned in this case is like chess, caro (gomoku), Othello online game. Each player makes a move in his turn, submit to host, and wait for other players turn.

Desired criteria:
+ Target: mobile
+ Simple, design for max 4 players per game
+ Keep server in low cost (CPU, bandwidth, transfer rate)
+ Available auto-reconnect if any

So I decide to use UDP. Server works like client discovery and message delivery at all. There’re two problems need to solve:
+ Package transmission mechanism: use UDP punch hole
+ Turn-based mechanism

For turn-based controlling, I have an idea as following:
+ Suppose that a host places two roles: host and client too
+ Client works in 3 states: LOCK (not in turn), INPUT (in turn), and SYNC (submit data to server), with two important properties: seq (sequence index), and turnIndex (turn index of each client)

Host work flow:
seqNo = 0
userInTurn = 0
while (true)
    if receive_submit() then
        seqNo = seqNo  + 1
        userInTurn = get_next_turn()
    deliver_to_all_clients(seqNo, userInTurn, gamedata)
Client work flow:
seqNo = -1
function On_Receive_Data_Update(ServerSeqNo, currentTurnIndex, gamedata)
    if ServerSeqNo >= seqNo then -- valid seq
        if ServerSeqNo > seqNo -- change state
            if state is LOCK then
                if currentTurnIndex == turnIndex then
            elseif state is INPUT then
                if currentTurnIndex != turnIndex then
                if currentTurnIndex == turnIndex then
function update_client_control()
    if state is INPUT then
       if hasEndTurn() then submit() end
Please see full post with figure at

This is only a theoretically mechanism. Point me out if I make any mistake . Thank you in advance!

Enjoy our games at:
03-03-2013, 07:10 AM
Apple's GameCenter has support for turn based games out of the box. This may save you a lot of effort. In any case, I don't see the point of using UDP here but I don't think anybody here will go to a technical level explaining stuff.

03-04-2013, 12:16 AM
KISS keep it simple you know what

Apple's turn based API also means you don't have to provide servers.

UDP might look inviting for this but by the time you finish your implementation you will have created a connection based protocol. So in the long run it's more work than any other method.


My iOS games: JiggleSaw
My Blog: JiggleSaw development
03-17-2013, 12:54 PM
Apple API in this case is a good point, I'm learning to use it. But it's not cross-platform

Enjoy our games at:
03-17-2013, 04:34 PM
If you want a cross-platform solution:

You can loose packets in UDP.
You can ask why all game use UDP intead of TCP ? TCP is a lot more reliable than UDP.

UDP has better performance, put lack of reliability like TCP.
In video game they just do two types of UDP connexion:
- Simple UDP where information can be lost (not sensitive information for the player like statistics or are re-send often like his position )
- Enchanced UDP where they build a mecanism to increase reliability on top of it.
(they try to have almost the same reliability than TCP but with better performance)

Doing your own enchanced UDP will take you several weeks.
For a turn by turn game you should choose probably TCP. It's not an FPS and you don't need high performance. It will be a lot simpler to do, and reliable for your players with bad connections.

On games (xbox360/PC/PS3) I've worked on, I've used Raknet to do everything low level related (punch through, udp reliable etc..). I don't know how much it can cost.

Another advice:
You can also use a master server to create game and help a client to connect to each other, but after that you could use one of the client as a server.
Like that you can scale much more. Most recent game (even AAA FPS) do this.
But it's more complicate, and you can implement it later.
03-17-2013, 11:46 PM
You could probably just use enet

Well proven game networking lib, runs on all platforms.
Does TCP and UDP + data protocol on top...
03-18-2013, 12:06 AM
Thank @Hercule, a lot of things to do with UDP, I'll take account, and do a testbed for some scenarios, maybe UDP for discovery & TCP for p2p connection in game
@TheBunny: thank for share, I' cloning , hope I could understand source code quickly

Enjoy our games at: