Game Development Reference
In-Depth Information
Transport layer - TCP versus UDP
The chances are that if a person is dealing with the transport layer, they will opt to use
either TPC or UDP to achieve their goal. In essence, both protocols exist to achieve one
thing—deliver data from one machine to another over a network. The differences come in
reliability and performance.
When we try to send data over the internet (in the form of a packet), the IP ( Internet Pro-
tocol— Network layer) tries to guide the data the best it can so that it arrives at its destina-
tion. The destination of each packet is marked by an IP address, which represents the des-
tination in the form of a number (IPv4 uses a 32-bit field, whereas IPv6 uses a 128-bit one).
However, the protocol doesn't guarantee that a packet will arrive at its destination at all (it
may get lost along the way), or if it does arrive, the data in it can be damaged. These prob-
lems are tackled by TCP and UDP, which are built on top of the Internet Protocol.
The Transmission Control Protocol ( TCP ) is connection-oriented and it's used when we
want to ensure that our packets arrive at their destination intact and in the correct order.
That makes the protocol reliable . In order for that to happen, the protocol has to send a lot
more information with the original data, because it tracks each and every packet. If a pack-
et gets lost, the protocol waits until a new one is sent over before continuing. This ensures
that when we send packets {1, 2, 3, 4} they will arrive in the same order: 1, 2, 3, 4.
On the other hand, User Datagram Protocol ( UDP ) is unreliable and does not track if a
packet goes missing or if the order of the packets is wrong. This technique requires much
less memory for each packet and doesn't require any wait time for lost packets, because the
protocol doesn't know if a packet got lost along the way.
When considering which protocol to use, we should ask ourselves one question, "can we
wait for lost packets or not?" In many cases the answer to that question is yes. Most things
on the internet use TCP—all web pages (HTML), e-mail, business applications, transaction
applications, and so on. All of those have to guarantee that the data which was sent from
one end is safely transferred to the other and they can afford to sacrifice a few milliseconds
of wait time to achieve that. Some games fall into the same category. Genres such as turn-
based strategies, card games, point-and-clicks, and even some puzzlers use TCP with no
problems. In most cases though, when we have a lot of objects that need to be synchronized
on all clients at the same time (action games, first person shooters, real time strategies),
TCP can slow down the game by having to wait for packets (which we might be ok with
losing—for example, some position updates can get lost with no impact). Also the addi-
Search WWH ::




Custom Search