I've been looking on the internet, but I can't really find any good answers to all my questions..

I started to think about writing a little multiplayer game on my own and after thinking and thinking and thinking about how I should begin I came to the conclusion, that implementing the network communication part at first wouldn't be a bad idea since the testing part becomes "easier".

So my first question is:

Is there any library that would make my life easier if I wanted to write the server part in C++?

I just don't know how one would implement the communication between server and client. Imagine a game that is made up of blocks (like minecraft if you want) but the client should only receive a tiny part of that map. So my question at this one is: Would you send raw byte data? Or would you use more abstract formats like XML? Let us assume that this should work with "many" clients as well..

And here is another thing..

Would one use either a single thread that collects all connections and sends requests to specific worker threads or would you create a new thread for every single connection that is established to the server?

  • 3
    \$\begingroup\$ Tip: Use the same language for the client & server if possible \$\endgroup\$ Commented Jan 29, 2013 at 19:16
  • 3
    \$\begingroup\$ Try to get your questions more concise and read up on the types of questions to ask here. Which technology to use questions aren't on topic for this site. Threading will depend on your requirements. Check out the FAQ for a list of sites that deal with getting started and discussion oriented questions. Your questions here are reasonable ones for someone just getting started, but they're not really on topic for this site. \$\endgroup\$
    – House
    Commented Jan 29, 2013 at 19:19
  • \$\begingroup\$ @JohnMcDonald I thought I could write the server part in C++ since it allows to code more efficiently. On the other hand I was thinking, that Java allows the client to run architecture independent which I wouln't mind. :) Could you name any specific reasons why I shouln't decide it like that? Byte56 Sorry, I know my question isn't quiet precise.. \$\endgroup\$ Commented Jan 29, 2013 at 19:36
  • 1
    \$\begingroup\$ Using one language means you can share code. If your client is Java, there's little reason to think your server needs C++. Java works just fine for most server needs. \$\endgroup\$ Commented Jan 29, 2013 at 19:40
  • \$\begingroup\$ Like Sean hinted at: writing code once in Java will save you valuable time. There will likely be large chunks of shared code that you would have to write twice if you used two languages. \$\endgroup\$ Commented Jan 29, 2013 at 20:06

4 Answers 4


One library to use is ENet, which implements a generic network API suitable for many games. Or libevent even, which just handles the low-level network events.

There is not a direct need for threads on most platforms, though Windows forces their use for large numbers of clients. Separate threads can be useful when you need a fixed response time or a constant packet send rate, less so for less action-y games.

Send raw bye data, compressed/packed as best you can. It lets clients with worse connections play better, is faster, and keeps your server bandwidth costs down. Some platforms (not yours most likely) have requirements that the game be playable at some low bandwidth like 128kbps, so packing your packets efficiently is a good skill to cultivate. Good game network programmers tend to get hired quickly and paid towards the higher end for game devs, and knowing how to keep bandwidth usage low is part of that skill set. XML is great for a number of reasons, but being small isn't one of them.

Packing mostly comes down to only using as many bits as you need and no more. If all your tiles can be enumerated with 4 bits, pack two tiles per byte. It's a bit more work when things don't line up nicely, but not much. If you use floats, consider writing the code for 16-bit half-precision or custom precision and use that as appropriate (its only a few lines of code - I have a simple template that supports arbitrary mantissa and exponent sizes and optional sign bit that I can also use for half-precision; converting to and from single and double precision is super simple.)

It can be helpful to have a bitstream (vs the usual byte stream) buffer that you can read and write values to.

Actually handling the networking part differs heavily by platform, which is why libevent is useful. Best recommendation there is to not worry deeply about it unless you want to be a pro game network coder. If you so want to learn, your best bet is to just read a lot of books and online docs on hoe each OS's network stack and API works. Making Linux work well is very different than an XBox, for instance.

Only send meaningful changes. If an object is not moving, there is no need to send position. Likewise, if the client he already been sent a chunk if the world and it hadn't changed, don't send it again.

Use some form of Area of Interest filtering to send only relevant things to each client. The client cant see an object 10 miles away, so don't send updates about that object. Just send updates about nearby objects.

As always, read the Gaffer on Games networking articles for a good light introduction to networking games.

  • \$\begingroup\$ Thank you very much! This one contains a lot of usefull information :) I really didn't know if it's a good idea if I start writing my own data-packets which I send over network or if I should use XML to handle stuff like that. But I am glad that you confirmed my guess that it wouldn't be such a bad idea encoding network messages as I need them.. ^^ \$\endgroup\$ Commented Jan 29, 2013 at 19:48

To answer your first question, yes there is. In fact there is a lot of libraries available for network programming in C++.

Since you're writing a game already then I'd suggest using the sockets from SFML or maybe even use SFML to create your game.


On the other hand you could always take a look at boost or qt as well.

About the packet usage, always think about speed and peformance. Sending in XML format would require larger packets which would decrease the speed and performance and may/may not create lag.

About threads for the connections. Well as I have already suggested SFML then I'd just referre to the selector they have: http://www.sfml-dev.org/tutorials/1.2/network-selector.php

Also writing both the client and server in the same language would be far the best idea in my opinion, because first of all you can share your code much easier, handling packets will be a lot easier as well, because you could just use the same packet layout over and over. As well cryptography implementation would be way easier. You would also end up doing a lot of double work.

  • \$\begingroup\$ Thank you for that hint. Looks very interesting. I've red some opinions about selectors and most of them said, that if one wants to take a lot of single connections a selecting approach should be prefered. I can understand your argument about that C++/Java thing.. but still.. dosn't it give you a lot of time back if your client runs on every platform? \$\endgroup\$ Commented Jan 30, 2013 at 20:43

I understand you said you wish for the server to be in c++, but since you tagged Java I will go with that instead as I do not have experience with c++.

Library called Kryonet is designed for games and provides a server and client implementation and can serialize any class and send it across the wire also works on android with LibGDX which for me is a plus.

Transfer of data can be either XML or data if you are going with sending chunks of the map (like minecraft) then the raw chunk file would be best. Updates on the other hand would need a structure to be sent (Note: Kryonet will allow sending of a file and sending of a class which gives you both raw and structured you would need).

As for your other questions, I will let others answer as Kryonet pretty much abstracted a lot of those issue so I yet to encounter them.

  • \$\begingroup\$ Sorry, I missed to mention that I have in mind to write the server in C++ and the client in Java. \$\endgroup\$ Commented Jan 29, 2013 at 19:38

Reaching every platform is crucial. Linux and Mac geeks are enthusiastic about games that support them from day one. Speed up your game after you get at least one non windows (Android, Dream, Linux, iOS) version up.

Structure your data in XML, then worry about compressing it to efficient packets, later. In most cases, a generic compression of XML is good enough. If not, optimize the piggy high performance bottlenecks.

C++ is for pros. Consider something friendlier for your first game. A lisp is good. Java is good. CPU speed / compiled code will not be your bottleneck, in 2017. Your network will.

Map out every millisecond of user time, and be very wary of any delay over 400ms for any user action.

  • 2
    \$\begingroup\$ "C++ is for pros. Consider something friendlier for your first game. A lisp is good. Java is good. CPU speed / compiled code will not be your bottleneck, in 2017. Your network will. " - I entirely disagree with this statement. C++ may not be a good start for an MMO, but it is certainly not "for pros"; if it was, generic programming courses would not start new programmers off by teaching them C++. Furthermore, I can tell you full stop that yes; CPU speed can very much be a bottleneck in 2017. \$\endgroup\$
    – Gnemlock
    Commented Jan 21, 2017 at 22:22
  • 1
    \$\begingroup\$ "Reaching every platform is crucial" - I disagree with this; it completely depends on your target audience. "Speed up your game after you get at least one non windows" - I'm not sure what you're implying there, so performance isn't necessary on Windows? "Structure your data in XML" - Is this the 90s? There are far better data structures than XML ex. json for example; I'd even argue that isn't that well to use either. If you have the possibility you should store data in binary, since it's way faster to load/save. Depending on the data you can use json or anything alike ofc. \$\endgroup\$
    – Bauss
    Commented Oct 15, 2017 at 13:16
  • 1
    \$\begingroup\$ "C++ is for pros" - C++ isn't for pros only. The only hard concepts about C++ are its meta programming capabilities; heck I won't even say they're that complex to grasp once you have basic programming knowledge. "A lisp is good. Java is good" - This explains your whole answer so far. \$\endgroup\$
    – Bauss
    Commented Oct 15, 2017 at 13:18

You must log in to answer this question.

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