Find a course

Module 3: Moving DataLesson 6 of 1012 min

TCP, UDP and Ports

Learn how TCP guarantees reliable delivery with a three-way handshake, why games and calls often use UDP, and how port numbers reach the right app.

Step 1 of 11Learn

Lost packets happen

The internet is a "best effort" system. IP gets packets moving toward their destination, but it makes no promises. Packets can be dropped when a router is overloaded, corrupted by interference, duplicated or delivered out of order. On a busy network, losing a small percentage of packets is normal.

For some data, that's unacceptable. If a few packets of a downloaded app go missing, the app won't install. If part of an email disappears, the message is garbled.

For other data, waiting is worse than losing a little. In a video call, a packet carrying a tiny slice of audio that arrives half a second late is useless; the conversation has already moved on. Better to skip it and play the next one.

That's why the internet has two main transport protocols that sit on top of IP:

  • TCP (Transmission Control Protocol): reliable, ordered delivery. Slower to start, but every byte arrives, in order.
  • UDP (User Datagram Protocol): fast, lightweight, no guarantees. Packets are sent and forgotten.

Applications choose the one that fits their needs. Understanding the difference explains why a game can feel laggy while a download still finishes perfectly, and why your video call goes pixelated instead of pausing.

Step 2 of 11Practice · Sort into groups

Which transport protocol fits each use better?

Tap an item, then tap its group (or drag it there).

Step 3 of 11Compare

TCP vs UDP

AspectTCPUDP
DeliveryGuaranteed, in orderBest effort, no resend
SetupHandshake firstJust send
SpeedExtra round trips and waitingMinimal delay
Good forWeb pages, email, file downloadsVideo calls, live games, DNS queries

Step 4 of 11Learn

How TCP makes delivery reliable

TCP builds reliability on top of unreliable IP using a few clever tricks.

1. The three-way handshake. Before sending data, the two computers set up a connection:

  • The client sends a SYN ("synchronize: I'd like to talk").
  • The server replies SYN-ACK ("got it, I'd like to talk too").
  • The client sends an ACK ("acknowledged, let's go").

This costs one round trip before any real data flows, which is one reason latency matters so much.

2. Sequence numbers. Every byte of data is numbered, so the receiver can put packets back in the right order and spot gaps.

3. Acknowledgments and retransmission. The receiver regularly confirms what it got. If the sender doesn't receive an acknowledgment in time, it assumes the packet was lost and sends it again.

4. Flow and congestion control. TCP starts sending slowly and speeds up as acknowledgments arrive. If packets start getting lost, which often signals a congested network, it slows down. This automatic politeness is part of what keeps the internet from collapsing under heavy load.

UDP skips all of this: no handshake, no numbering guarantees, no retransmission. Applications that use UDP handle any needed fixes themselves. In fact, the newest version of the web's protocol, HTTP/3, runs over a protocol called QUIC, which is built on UDP and adds its own faster reliability features.

Step 5 of 11Practice · Put in order

Put the TCP three-way handshake in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Data transfer begins
  2. Client sends SYN
  3. Server replies SYN-ACK
  4. Client sends ACK

Step 6 of 11Practice · Diagram

The three-way handshake

After which message can the client start sending data?

Tap the shape that answers the question, then press Check.

The three-way handshakeProcess diagram: Client: SYN → Server: SYN-ACK → Client: ACK → Data flows.Client: SYNServer:SYN-ACKClient: ACKData flows
Client: SYN

"I'd like to talk; my starting sequence number is X."

Server: SYN-ACK

"Got it (X+1). My starting number is Y."

Client: ACK

"Got yours too (Y+1)." The connection is now open.

Data flows

Each segment is numbered and acknowledged; missing ones are resent.

Step 7 of 11Learn

Ports: reaching the right app

Your phone may be running a browser, a music app, a messaging app and a game, all receiving data at the same time through one IP address. How does incoming data reach the right app?

The answer is port numbers. TCP and UDP headers include a source port and a destination port, numbers from 0 to 65,535. If the IP address is like a building's street address, the port is like an apartment number.

Servers listen on well-known ports so clients know where to knock:

PortService
53DNS
80HTTP (unencrypted web)
443HTTPS (encrypted web)
25SMTP (sending email between servers)
22SSH (secure remote login)

Your device picks a random high-numbered temporary port for its side of each connection, so the response comes back to the right app. The combination of IP address and port identifies one end of a conversation.

Firewalls often work by port: a school network might block certain ports used by games or file-sharing programs, while leaving 443 open for the web.

Step 8 of 11Practice · Match the pairs

Match each port number to its usual service.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. 53
  2. 80
  3. 443
  4. 22

Step 9 of 11Compare

Layers of the internet

AspectWhat it doesExamples
ApplicationWhat apps speak to each otherHTTP, DNS, email protocols
TransportDelivery between programs, using portsTCP, UDP
InternetAddressing and routing between networksIP (IPv4, IPv6)
LinkMoving bits across one physical connectionWi-Fi, Ethernet, cellular, fiber

Step 10 of 11Practice · True or false

UDP guarantees that every packet arrives and in the correct order.

Step 11 of 11Practice · Choose one

Why does each connection need a port number in addition to an IP address?