UDP vs TCP

From DmWiki

TCP

TCP is probably the most commonly used protocol, simply because it is used for so many applications such as HTTP, POP, SMTP, etc. TCP is a protocol which guarantees that the receiver will receive exactly what the sender sent - there will be no errors, it will be in the correct order, everything will work just fine.

Well that's great, but how exactly can TCP guarantee that? Quite simple really. If a packet does go astray and doesn't arrive, then TCP resends it. However, although this means everything arrives correctly, it doesn't mean it arrives fast. If there's a lot of packet loss, the overall message could take a while to arrive. That's why some games don't use this protocol.

What TCP actually does is cut down the speed by 50% whenever a packet is lost, but it increases it slowly when packets are successfully received. This avoids congestion (the first TCP didn't have this, and congestion was a major problem), but a couple of lost packets can really slow things down.

One other thing to note about TCP is that it is a connection-based protocol. One program on one socket connects to another program on another socket (probably on another computer).

TCP is obviously useful in non-time critical applications where you definitely want things to work. In a game, you might want to use TCP for any chat you might have, so that it is sent and received correctly. You might also want to leave it open for any important, but not fast, message needing to be sent.

UDP

UDP is similar to TCP in that it is a protocol for sending and receiving packets across a network, but with two major differences. First, it is connectionless. This means that one program can send off a load of packets to another, but that's the end of their relationship. The second might send some back to the first and the first might send some more, but there's never a solid connection. If one just stops sending packets - that's fine.

UDP is also different from TCP in that it doesn't provide any sort of guarantee that the receiver will receive packets in the right order; in fact, there's no guarantee that the packets will arrive at all. All that is guaranteed is that the packet's contents, if they arrive, will arrive exactly as sent, without data corruption.

UDP is much faster than TCP because there's no extra overhead for error-checking above the packet level. For this reason, games that require constant fast transmission of data (such as player coordinates in a fast-paced shooter) will use UDP. When sending several position updates per second, it's okay if an occasional packet is dropped or received out of order because the movement will be back on track in a split second. However, UDP should not be used where reliability of data matters, such as with a classic RPG. It is fine if one bullet goes missing or one step is off in a shooter, but it's not okay if an item is not picked up or an attack is not executed due to a dropped packet; therefore, in games that need 100% reliability, TCP is the way to go.

There are several libraries (such as RakNet) that create their own reliability layer over UDP, effectively making it as reliable as TCP but with the speed benefits of the UDP protocol. They implement their own packet headers, numbering each packet, and when packets are received out of order they are simply arranged according to their numbers. Similarly, when a packet is missing, there is an obvious gap in the number line, and that numbered packet is requested for retransmission. All of these details are handled automatically by the network library, and the programmer can treat it like TCP.

External Links



DevMaster navigation