Thursday, September 11, 2014

UDP vs TCP

When making an online multiplayer game, how the networking is done between clients is an important decision. What should be sent between clients and how they interact with each other will define how your game works. How this information is sent between clients is also a big decision, which can effect how well your game plays online.
TCP and UDP are the two most common methods of packaging and transmitting the necessary data between clients, or a client and a server. TCP is much more reliable, It checks itself as is passes along the various routers and switches, and if a piece of data is lost, stuck behind, or out of order, it will wait until it is complete again. However, compared to UDP it is slower because of these checks. UDP doesn't have the checks, if something is lost or left behind, it doesn't care. All the data that reaches its destination is what you get, in the correct order or not. It's a fire and forget method.
it20.info
For an online multiplayer, both speed and reliability are important, so which is better? Most games will actually use both. For example, League of Legends. League of Legends (LoL) is a Multiplayer Online Battle Arena (MOBA). Five players make up each team and they fight on Summoners Rift, the main map and game mode, to destroy the base of the opposing team. LoL uses UDP for pretty much all it's gameplay data. However, players statistics are sent using TCP. In a game MOBA, lag can make or break a single team fight, so UDP is preferred for it's speed. The stats that let you know the guy your facing is 12/1/4 (Kills/Deaths/Assists) doesn't require that speed because you can check it when you need to.
The game can still lag, but it's more often than not due to the ISP of the player and not because of UDP or TCP slowing it down. Lost packets are another potential issue, since it does use UDP, but it's very rare and would only impact fights.
In the end, both TCP and UDP are have strengths and weaknesses. Which to use should be taken on a game by game basis. Using both is also a viable option if you have systems that would benefit from it.