I am developing a multi-player on-line game.

I just started coding the server but I have no idea how to do it. Do I have to use threads ?

And if i do, do I need one thread for every client?

Knowing that I am using UDP, can I use multiple sockets in different threads to send packets on the same port?


server does:

-listen for client connections

-when a client connects:

    -update files (send new files to the client)

    -retrieve data from database

    -loop to retrieve and update live data

-when client disconnects:

    -update data in database
  • 6
    \$\begingroup\$ Not strictly an answer, but: you should strongly consider starting with a working MMO server framework, and then altering it to your specific needs… \$\endgroup\$
    – BRPocock
    Dec 28, 2011 at 16:28
  • 2
    \$\begingroup\$ If you have no idea, use SmartFoxServer or something \$\endgroup\$
    – Nevermind
    Dec 28, 2011 at 17:48
  • 1
    \$\begingroup\$ I just save all "Player" objects in an array and I'm doing pretty fine... A "Player" object has a property called "connection" trough which you can communicate. \$\endgroup\$
    – jcora
    Dec 28, 2011 at 18:04
  • \$\begingroup\$ -2 and 7 favorites lol \$\endgroup\$
    – jhocking
    May 28, 2013 at 15:04

1 Answer 1


I may be wrong, but your question makes it seem like you are missing a lot of knowledge in order to successfully write an MMO server. I know this message will likely fall on deaf ears because I was in your position when I started programming.

My answer: If I were you I would start smaller. If you want to learn to write an MMO server I would do the following.

  • Write a TCP based p2p chat client.
  • Extend that chat client to work with NAT routers
  • Extend the chat client to have a central server that authenticates and stores message history
  • Extend it to have a secure handshake with the server to verify the client software and server software/location
  • Write a high level architecture of what you would want your MMO server to be
  • Read some articles on MMO server architectures
  • Start expanding your high level architecture into more and more detail
  • Write essential user stories for your architecture
  • start implementing your server

The answer you probably want:

  • Write a thread for the listener to accept incoming connections using TCP
  • Once the player is connected, use TCP for chat messages and sector changes
  • Use UDP for in-sector movement
  • Each connection gets its own thread for the messaging
  • UDP can be implemented with threads (if you really want) but I would probably use some sort of queue on one thread that accepts movement messages. Or spread it out depending on how many connections you get.
  • \$\begingroup\$ I don't get some of the points, but i will go through all of them one by one to understand them and do whatever is necessary to finish the server. \$\endgroup\$
    – rxjsisfine
    Dec 28, 2011 at 15:38
  • \$\begingroup\$ "Each connection gets its own thread for the messaging" Note that this is the easy way to do it but it doesn't scale well at all. If you expect more connections per server than cores available on the hardware, you'd better invest in learning about task-based concurrency (or thread-pools if you prefer). \$\endgroup\$
    – Klaim
    May 28, 2013 at 14:32
  • 1
    \$\begingroup\$ I'de venture to bet that anyone reading this isn't in a place where scale matters. Simplicity and completing something are a lot more important \$\endgroup\$
    – brandon
    Feb 4, 2014 at 20:41

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