Network protocol for Games

Posted on 2003-12-02
Last Modified: 2013-12-04
I'm eventually interested in programming a small netcode for a massively multyplayer game (FPS).
I need to know some basics on the protocoll development for games:
Which protocol (UDP / TCP)?
Anything special for speeding up the communication.

Also I would like to know how to send the data across the net... (sending raw structs read from memory, or some higher data encapsulation such as plaintext).
Question by:Snyke
  • 3
  • 3
LVL 11

Accepted Solution

bcladd earned 50 total points
ID: 9863589
(1) How games send data across the wire varies, game to game. UDP is cheaper than TCP since packets don't have to be buffered and delivered in order: less overhead in the packets used (compared to payload) and less latency in getting information to the distant process. Disadvantage is no delivery guarantees.

(2) Speeding up the connection: The fastest packet is the one you don't send so send as little as you can get away with. What is the range:
(a) Client/Server model where all input is sent from client to server, all calculations done on the server, and all data for output returned to the client for rendering.
    Lots of data probably comes back down; scalability problems but every client has the exact same picture of the game.
(b) Peer/Peer model where only input signals are replicated, machine to machine, and each runs the full game with all calculations done locally.
   Lots less data; scales well; clients are more losely coupled AND can be used for cheating.

(3) Data across the net: You can send raw  data using pointers at structs/classes IF you know that all machines will be using the same processor (don't include Mac and PC versions of your code) and you can afford to send the whole struct every time a single field changes. Alternative is to send some sort of tag (indicating the object and the field) and then ONLY the changed value. Only makes sense if the tags are smaller than the remainder of the object.

Just some thoughts. Hope it helps, -bcl

Author Comment

ID: 9866643
Is there a possibility to, say build a game for Linux and then build an interpreter function on other processors?
Would be nice to have no processor limits...
I think a hybrid version of your A and B is the best: game relevant data is to be calculated by the server and the clients only calculate the visualization and irrelevant stuff. There's still to define what is game-relevant and what not...
LVL 11

Assisted Solution

bcladd earned 50 total points
ID: 9866820
The limitation is if you send byte sequences EXACTLY as they appear in memory. You will read (or have read) about network byte order? I can never remember whether it is little-endian or big-endian (Intel is, I think, big endian, PowerPC/Motorola are little endian and network byte order is little endian; in any case network matches Mac). The point is that all machines that have sockets have data marshalling functions that translate host formatted multi-byte quantities (integers of various types for the most part) to network byte order and others to translate network to host ordering (htons, for example, takes a short in host order and returns a short in network order; reverse routine is ntohs). Passing information in cannonical network order is useful if you expect to have many different types of machines communicating.

It is a pain if you only really expect to have one architecture (or mainly one architecture) and you know it isn't using network byte order internally: every int you pass is translated to and from network byte order FOR NO REASON. The fix is to choose your own cannonical ordering. Then, if you add a machine architecture that is different you make that machine's version translate to/from your main machine's byte order.

Calculating what is game relevant is hard. The problem is made harder by the fact that it is dynamic. Think about it: If the client is primarily for rendering sights and sounds for me then there is no need for it to get any updates about what the postman is doing as he loads his truck downtown. However, just before he gets to my door I might hear his steps on the walk, his knock on the door, and, opening the door I would want to kill him where he stands (this is an FPS, after all). Assuming I miss and he continues on his way, he will fade from relevance when he goes around the corner of the house...or will he? What if he just wanted cover and is going to double back in 1.2 seconds to fix me good?

Relevance is important because it limits the number of objects that need to be updated and so limits the required bandwidth.

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 9866938
Well I would be using a struct to describe the entire player and update it all at once. This way I have a consistent image of where the players are and what they are doing. I always thought that using htons on systems that internally use network styled memory will return without doing anything...
LVL 11

Expert Comment

ID: 9867023
Correct. If you happen to match netowork byte order hton* and ntoh* are effectively nops.


Author Comment

ID: 9931478
Really good answers all of you... couldn't decide which answer to accept but since bcladd answered the first question his one's accepted.

Expert Comment

ID: 9941453
Even though it has already been answered, here's how I solved the problem (and got a very lean and efficient network code).

First, use UDP. Build your own network message stack and your own syn-ack management. Not every packet "has" to reach its target (i.e. a packet where you update the player positions need not be ack'ed, there's no point in resending it. If it doesn't arrive NOW, it isn't needed anymore. In a second, the packet is worthless. A packet that sends e.g. the information that the player died or one that transmits a message MUST be resent to make sure it arrives. Whenever.).

What I did was to build a "pseudo transmission control" on UDP. I used the first byte of the transmitted data for a packet-ID and the second as a flag field, telling the other computer whether this packet needs to be ack'ed, if this packet is an ack (and if, the 3rd byte was the ID of the packet ack'ed) and a few more state related things.

This way you get almost the same reliability of TCP without the bloated overhead.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Very bored unfinished Death Star needs revitalization - Java GIF recycle 7 251
teenSum java challenge 7 89
help with BB8 Star Wars toy 4 117
Need a nodal sequencing tool 3 110
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question