🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Starting out with networking

Started by
8 comments, last by oliii 8 years, 3 months ago

I'm going to start out learning networking, with the idea to make multiplayer online games of various kinds. I have made a bunch of local games in GDevelop (2d game engine). Are there benefits to learning networking in raw C++ or using something like boost:asio? Or would it make little difference and should I just learn JSON (which GDevelop uses for multiplayer)? Also, are there any books, tutorials etc you would recommend someone starting out with networking should be reading/doing?

Advertisement
There are really three separate concepts:

1) Networking: IP, TCP, UDP, routing, latency, connections, ...
2) Distribution: Partition, timeout, discovery, authentication, ...
3) Games: Simulation, latency compensation, matchmaking, replication, ...
4) Specifics for your particular OS: I/O completion ports, kernel queuing, ASIO library, worker threads, java.nio, ...
5) Off-by-two errors.

A library built into a game engine is going to have to have made certain decisions regarding all of these layers.
As long as those decisions match the requirements of your game, that'll be fine. If you're trying to build a game that's different from the library's sweet spot, you'll be in trouble.
For example: Unreal and Unity both use a non-deterministic physics engine, which means that games with simulations based around lock-step deterministic simulation simply won't work in those engines.

To learn about 1), "TCP/IP Illustrated" by Stevens is fine. It will use C/C++-level "raw" sockets for examples, which is fine.
To learn about 2), there is a fair bit of literature related to batch processing (databases, enterprise IT, scientific computing) but precious little about real-time applications. Try "Networked Virtual Environments" by Singhal et al.
To learn about 3), it really depends on the genre of your game. There are various network blogs that help ("Zen of networked character physics" for example, or "The Source Networking Model")
To learn about 4), there are various tutorials and reference pages. The important part there is to already know the other bits, so you know what to look for, and also to know what your platform really is :-)

Note that both 1) and 2) are fairly old at this point, but luckily, the fundamentals of networking haven't changed in the last 20 years, so they're still fine.

JSON is just a way of encoding data. It's very popular for web services and configuration files because it's less inefficient than XML. It could work fine as an encoding method for slow-paced or few-player games, too. But, JSON has really nothing to do with networking, per se -- you could just as well use it for your save-game implementation. (Actually, it turns out that understanding about save-games helps with understanding about networking, too ...)
enum Bool { True, False, FileNotFound };

I personally would not use boost asio as a learning tool. It actually impedes learning as it has abstracted away most of the learning that could be had by struggling with the underlying sockets and such. There are definite benefits to learning the underlying constructs as you will learn that writing portable code is hard (winsock, epoll, kqueues, etc) and you often are really only targeting 1 or 2 platforms anyways. Additionally, the code base is very complex for a learner to go thru and furthermore you need to understand the trade-offs being made by the authors (design vs performance, etc). All of that is much weight if your goal is to just learn networking.

For me personally, I would go thru this if I wanted to learn basic networking.

You can play a bit with this also to get some experience : https://github.com/sp4cerat/Game-NET

Our team use C++, libevent and protobuf as network and serialization libraries, include architecture and logical module, maybe it can help you to learn how to program game server, see https://github.com/ketoo/NoahGameFrame

NFrame - agile game server developing framework. Github:https://github.com/ketoo/NoahGameFrame

I'm going to start out learning networking, with the idea to make multiplayer online games of various kinds. I have made a bunch of local games in GDevelop (2d game engine). Are there benefits to learning networking in raw C++ or using something like boost:asio?

If you're looking to make an online game in C++, go with boost.asio. Otherwise, since you'll probably build the game on Windows, you'll need to learn IOCP to get the full benefit of async network I/O. Then you'll realize a Linux server will be a more efficient and secure than a Windows server, and want to change over... good luck porting the core of your server, written in IOCP for Windows, to Linux (probably using epoll for async I/O). boost.asio wraps those APIs (and the equivalent on other OSes) in a portable fashion. boost.asio's "strand" concept is useful in non-IO contexts too, it allows you to schedule tasks that can't be performed concurrently in a safe manner, without stalling execution on a lock.
If you're not familiar with C++, though, developing a server in C++ is hard, even with the benefit of boost libraries. And last I knew there was no boost offering for managing the client's responsibilities (interfacing with the window manager, managing user input, drawing graphics, etc). Most of the system-specific and cross-platform APIs for that stuff, which are truly suitable for games as well, are written for C, which makes using them a bit harder. You might be better off at least prototyping in GDevelop if you've never used C++ before.

Also, believe it or not, C# might be a better option than C++ for making an online game. It's highly portable (thanks to Mono), has asynchronous I/O, user input, and (really basic 2D) graphics without third party libraries, and OpenGL bindings are available.

EDIT:
I both agree and disagree with Henderson. If you've never written any server code before, working with boost.asio will familiarize you with many important concepts. But it abstracts away all the nitty-gritty details and, like he said, the code is very hard to read through. If you're trying to make a game without too steep a learning curve or devoting too much time to network infrastructure, boost.asio is great. If you want to learn the most possible, and are less concerned with actually completing the game, it'd be worth working at a lower level.
Also, never work with actual RAW sockets without good reason.

Apart from the overhead when compared to Sockets, are there any huge negatives with using a RESTful Web API? This seems like the cheaper route to go when you can pay for some Azure web api services instead of picking up an entire VM to host the server on.

I should clarify, that if you need real-time networking like an FPS shooter, i see the need for a full socket server. For games like a multiplayer chess game, social avatar game (2d second life styled) etc, would there be any major reason to avoid a web api for them instead of a socket server?

Sure, you could do that, but you could also just use the the Azure hosted service to serve as matchmaker for NAT punchthrough and get the best of both worlds for those types of games.
:)

Alongside Beej tutorials, I'd take a look at this too :

http://gafferongames.com/networking-for-game-programmers/

This is old school socket programming, simple and effective. But won't give you a lot of things other methods would.

Can look at Unity game engine, or whatever game engine will facilitate making your game, and raknet, which is a well developed open source network library.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement