Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

I know these questions are really basic ones.. If one of you guys knows a good tutorial I would be thankfull if he leaves a link to it here..

Thank you

share|improve this question
2  
Tip: Use the same language for the client & server if possible –  John McDonald Jan 29 '13 at 19:16
3  
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. –  Byte56 Jan 29 '13 at 19:19
    
@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.. –  Stefan Falk Jan 29 '13 at 19:36
1  
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. –  Sean Middleditch Jan 29 '13 at 19:40
    
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. –  John McDonald Jan 29 '13 at 20:06

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
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.. ^^ –  Stefan Falk Jan 29 '13 at 19:48

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.

share|improve this answer
    
Sorry, I missed to mention that I have in mind to write the server in C++ and the client in Java. –  Stefan Falk Jan 29 '13 at 19:38

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.

http://www.sfml-dev.org/tutorials/1.2/network-sockets.php

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.

share|improve this answer
    
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? –  Stefan Falk Jan 30 '13 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.