Network Programming 101: General Concepts

by potato

Why Network?

Key Concepts



Packets are chunks of information sent between computers on a network. This can be anything from game scores to movement data in an FPS game. Naturally, for real-time games, how quickly packets can be sent between computers can influence the gameplay experience dramatically. There are types of packets, and also multiple ways to transmit them from machine to machine. The three topics below are of particular importance:


Packets are either reliable or unreliable.

  • Reliable
    • Packet is guaranteed to arrive (though how long it takes to get there is not certain).
    • Because the sender needs to verify the receiver got the message, time and processing power are taken to ensure that transmission was successful and complete, and if not, request a retransmission. This can cause delays in the game.
    • Useful for low-frequency data (actions that are not done as often) such as changes in the game score, or moves in a chess game.
  • Unreliable
    • Sometimes referred to as a datagram.
    • Packet is not guaranteed to arrive at the destination.
    • Useful for very frequently updated information (player position, facing, etc.) Since this data will likely be useless by the time the sender discovers a failed transmission and re-transmits the data, there is simply no point in making it reliable. Likewise, the consequences of missing a single player position update might not even be noticeable.

Receive Order



More commonly known as lag, latency is the amount of time it takes for information to travel between the sender and the receiver, and then back again. Latency is a key issue in real-time game networking, as an excessively high lag will cause a delay in one player’s actions from being executed on other players’ machines.

Lag is sometimes hard to control as a game developer. It is highly dependent upon the internet service that the user possesses. In rare cases, faulty drivers or hardware can result in high lag.

In addition to the round-trip latency, lag can also refer to noticeably high packet loss. Packet loss requires the receiver to identify incorrectly received data, and request a re-transmission from the sender. This takes much more time than if the message arrived completely and correctly the first time, and will cause lag for the player. Worse yet, depending on your design, packets may be missed entirely in these situations, causing missing data (most commonly observed by sudden snapping in player movements or actions).


Network Schemes

There are many types of networking architectures common to games, but the two that deserve mention here are client-server and peer to peer.


  • In this method all players connect to a central server, which arbitrates all game decisions, leaving only non-gameplay oriented tasks to player computers. The tasks that server and client are responsible for differ by design, though for security purposes generally all gameplay-affecting decisions are made by the server.
  • The server is assumed to be trustworthy - that the decisions it makes are above questionability, and all clients will yield to the server’s decision should a disagreement occur.
  • Pros:
    • Supports high number of players due to server broadcasting - less traffic across the network overall.
    • More secure, as the server is assumed to be trusted and thus sets a standard for correct information.
  • Cons:
    • The game must discontinue if the serving computer quits. This can disrupt games if the serving player quits the game.
    • Amount of decisions the server must make may cause poor performance if servers run on slower hardware.
    • If the serving player is on a slow internet connection, all players connected will suffer poor performance.
    • The serving player may cheat, detection of this may be difficult, but not impossible.

Client-server architectures are by far the most popular in most games, such as Counter-Strike, Battlefield, etc.

Peer To Peer

  • In this method all players in a game connect to each other. A scheme must be devised to validate each client’s actions’ legality with other players to ensure non-cheating.
  • Peers are assumed by each other peer to be untrustworthy and possibly cheating.
  • Pros:
    • Will not disrupt game if any one peer drops out, the game may continue.
    • May minimize lag between players on fast internet connections, as opposed to forcing everyone to run at the speed of a possibly slow server.
    • Presents the opportunity to distribute processing between players - for games with heavy AI for example, this ensures that the processing load is split evenly amongst players instead of loading it all onto the server.
  • Cons:
    • Since each player must establish and send communications to every other player, the number of connections and the amount of traffic multiplies geometrically as player count increases. This puts a relatively low cap on maximum supportable peers in a game.
  • Cheat detection and action validation may be more difficult - since each peer is untrustworthy, whose information do you go with if a disagreement occurs? The system may be more complex due to this.
    • Synchronization may be more difficult due to the different lags between players, as opposed to a single latency posed in client-server communications.

Peer to peer architectures are less popular, but useful in some cases, especially where games should not terminate due to the serving player quitting. Examples of peer to peer implementations include Starcraft and Doom 3.

Common and Popular Network Libraries

tutorialsarticlesandexamples/introduction_to_networking.txt · Last modified: 2013/11/22 13:32