TCP (Transmission Control Protocol) is a connection-oriented protocol and tracks all the data sent. It is a more reliable but slower network protocol. UDP (User Datagram Protocol) is a connection-less protocol that does not have the connection overhead that TCP does. It is lighter weight and faster, but not as reliable as TCP.
What exactly is networking?
Imagine you’re writing a letter to someone. “I would like to communicate with you,” you write. “Please verify.” Later, you receive a response: “I acknowledge your desire to communicate.”
“I acknowledge your acknowledgment. Would you like to hear a joke?”
“I am ready for the joke.”
“Here is the joke: UDP. With which protocol are you likely to receive packets out of order?”
“I have heard the joke. Consider me amused.”
In total, four hundred milliseconds have passed since you sent the first letter. Too slow, you think. Maybe you should try a different protocol.
Table of Contents
What is the Difference Between TCP and UDP Networking Protocols
When you want to communicate over a network, there are two main protocols used to make it happen: the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). Both of them use packets of data, and both are part of the Internet Protocol (IP) suite. So what’s the difference? The protocol you use when networking depends on whether you need a “reliable” or “unreliable” connection, respectively.
Do you need to confirm that the data reached the remote peer? Or would you instead sling bytes into the aether as fast as possible? TCP will verify that your data arrived, while UDP couldn’t care less. But for every packet of data UDP drops along the way, ten messages reach their destination in a fraction of the time it would take with TCP.
That’s a high-level overview of the situation. Now let’s take a closer look, starting with the bundles of bytes that make it happen: packets.
What’s a Packet in Networking?
Packets in networking are the digital equivalent of a mailed letter. They hold the data being sent, a header stamped with the IP addresses of both the source and the peer, as well as the packet’s number in series if it’s part of a larger whole. The header can also contain error-checking information to help the peer determine if the packet was ill-formed, in which case it may request that the packet be resent.
Everything you do online, from sending a text message over Discord to movements and actions in-game, are all sent across the internet in packets.
What is TCP?
The Transmission Control Protocol originated in the 1970s, near the dawn of the internet itself, as a way to handle internetworking between computers. There was an urgent need for communications protocols which verified that messages had been sent or received in a “reliable” manner. With TCP, the concept of the handshake emerged — the back-and-forth between two peers to establish an active connection for transferring data.
Packets in a TCP data stream are checked for duplicates and ordered as they arrive, with confirmation sent back to the source when packets are received. As you might imagine, the order of packets is vital in certain key scenarios. It wouldn’t do for pixels of color or text on a page to be flung around the page like leaves in a hurricane. Retransmission of packets occurs when they don’t pass inspection or aren’t confirmed, which can add precious milliseconds to each round trip.
In short, TCP is slow and steady. Perfectly functional for most purposes. But what if you have a need — the need for speed?
What is UDP?
The User Datagram Protocol was developed after TCP, first appearing on the scene in 1980. Unlike TCP, UDP is a connectionless protocol. It also offers a few novel features like data routing through specific ports and an additional checksum for error-checking. But as you might recall, UDP is what’s known as an “unreliable” protocol. Why would anyone want to introduce unreliability into networking?
The answer: speed.
UDP slings packets as fast as you choose to send them, instead of ensuring active connections between peers with handshakes and confirmations. UDP doesn’t even care if the peer is listening. The extra checksum can help verify data integrity, but without round-trip confirmations or error-checking, packet loss is inevitable.
Now that you know your TCP from your UDP, how do you choose which one to use?
Uses for TCP and UDP
TCP is the protocol that keeps the internet humming, handling most of the traffic across the web. Connecting to your favorite websites involves establishing a “reliable” active connection via handshakes and order-checking. Not the fastest way to exchange data, but it’s sure to arrive — and what’s a few extra milliseconds among friends?
Some applications can’t afford to add any delay, like livestreaming and online gaming. A few dropped packets here and there is a small price to pay for the blisteringly fast speed UDP affords, with the potential to transmit thousands of packets in a single second. Thanks to the magic of interpolation, things like character movement in games are relatively unaffected by data loss. Let’s hope you don’t lose packets for actions like firing your weapon along the way.
While it seems to be relatively clear cut, in practice, there’s a bit of flexibility. Any browser-based multiplayer game using WebSockets is sending and receiving data with TCP. And some desktop games, especially MMOs like World of Warcraft, choose to handle networking with TCP to accurately record player actions.
The next time you’re slinging frost bolts in WoW or hot on the heels of a killstreak in Call of Duty, now you know how TCP and UDP help to make it happen.
Bitten by the development bug? Here’s a few more articles to keep you going: