Find a course

Module 3: Moving Data: Transport and RoutingLesson 7 of 1312 min

TCP, UDP and Ports

Learn how ports identify programs, how TCP sets up reliable connections with a three-way handshake, when UDP is the better choice, and common port numbers.

Step 1 of 13Learn

Which program gets the packet?

A single server might run a website, an email service and remote login at the same time. An IP address gets a packet to the right machine. Something else must get it to the right program. That is the job of the transport layer (Layer 4) and its port numbers.

A port is a 16-bit number, from 0 to 65,535, that identifies a service or conversation on a device. Servers listen on well-known ports so clients know where to find them: web servers on 443 (HTTPS) and 80 (HTTP), SSH on 22, DNS on 53. When your browser connects, your operating system picks a temporary high-numbered ephemeral port, such as 51734, as the source port, so replies come back to the right browser tab.

The combination of an IP address and a port is a socket, written like 203.0.113.10:443. A connection is identified by four values: source IP, source port, destination IP, destination port. That is how a server can talk to thousands of clients on port 443 at the same time without mixing them up, and how your laptop can open dozens of connections to the same site.

Port ranges:

  • 0–1023: well-known ports, assigned to standard services.
  • 1024–49151: registered ports, used by many applications (databases, game servers).
  • 49152–65535: dynamic or ephemeral ports, picked by clients for outgoing connections.

Step 2 of 13Practice · Match the pairs

Match each common service to its default port.

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

  1. HTTPS (secure web)
  2. HTTP (plain web)
  3. SSH (secure remote login)
  4. DNS (name lookups)
  5. SMTP (sending email between servers)
  6. RDP (Windows Remote Desktop)

Step 3 of 13Learn

TCP: reliable, ordered delivery

TCP (Transmission Control Protocol) gives applications a reliable stream of bytes, as if there were a perfect pipe between the two programs. Underneath, it works hard to provide that illusion.

Connection setup: the three-way handshake.

  1. The client sends a segment with the SYN flag: "I'd like to connect; my starting sequence number is X."
  2. The server replies with SYN-ACK: "OK; I acknowledge X, and my starting number is Y."
  3. The client sends ACK: "I acknowledge Y." The connection is open.

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

During the connection, TCP numbers every byte. The receiver sends acknowledgments (ACKs) for what arrived. If an acknowledgment doesn't come back in time, the sender retransmits the data. The receiver puts segments back in order. Flow control stops a fast sender from overwhelming a slow receiver, and congestion control makes senders slow down when the network is overloaded, then speed up again.

When finished, each side closes its direction with a FIN segment. A RST (reset) aborts a connection abruptly, for example when you try to connect to a port where nothing is listening.

TCP carries web traffic, email, file transfers, SSH and anything else where every byte must arrive intact.

Step 4 of 13Practice · Put in order

Put the steps of opening a TCP connection and sending data in order.

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

  1. Server replies with SYN-ACK
  2. Client sends the HTTP request data
  3. Server acknowledges the data and sends its response
  4. Client sends SYN
  5. Client sends ACK

Step 5 of 13Practice · Diagram

The TCP three-way handshake, then data

After which message can the client start sending data?

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

The TCP three-way handshake, then dataProcess diagram: SYN → SYN-ACK → ACK → Data → ACKs and response.SYNSYN-ACKACKDataACKs and response
SYN

Client: "I want to talk; my starting sequence number is X."

SYN-ACK

Server: "Got X; mine is Y."

ACK

Client: "Got Y." The connection is open.

Data

The HTTP request travels in numbered segments.

ACKs and response

Lost segments are noticed and sent again.

Step 6 of 13Learn

UDP: fast and simple

UDP (User Datagram Protocol) is the minimalist alternative. It adds ports and a checksum to IP and nothing else: no handshake, no acknowledgments, no retransmission, no ordering. Each datagram is sent and forgotten.

That sounds worse, but for some applications it's exactly right:

  • Real-time voice, video calls and online games. A packet that arrives late is useless; it's better to skip it and play the next one than to pause everything waiting for a retransmission. Applications handle small losses themselves, for example by smoothing over a missing slice of audio.
  • DNS lookups. A query and an answer each usually fit in a single packet. Setting up a TCP connection first would double the time. If no answer comes back, the client simply asks again.
  • DHCP, streaming telemetry and many network discovery protocols use UDP for similar reasons.

Modern protocols also build their own reliability on top of UDP to avoid TCP's limitations. QUIC, the transport under HTTP/3, runs over UDP and combines connection setup with encryption, which saves round trips. Much of the traffic to large sites now uses it.

So the choice isn't "good versus bad". TCP trades speed for guaranteed delivery; UDP trades guarantees for low delay and simplicity.

Step 7 of 13Compare

TCP vs. UDP

AspectTCPUDP
ConnectionThree-way handshake before dataNone; just send
ReliabilityAcknowledgments and retransmissionNo guarantee of delivery
OrderingData delivered in orderDatagrams may arrive out of order
Overhead and delayHigherLower
Typical usesWeb, email, file transfer, SSHDNS, voice and video calls, games, DHCP, QUIC

Step 8 of 13Practice · Sort into groups

Sort each application by the transport protocol it typically uses.

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

Step 9 of 13Practice · True or false

If a TCP segment is lost, the receiving application sees a gap in the data unless it requests the missing part itself.

Step 10 of 13Note

Step 11 of 13Practice · Work it out

How many different port numbers can TCP use on one IP address (0 through 65,535 inclusive)?

Step 12 of 13Diagram

The port number ranges

The port number rangesScale: 443 HTTPS, 3389 RDP, 8080 alt web, Client side, e.g. 51544.010k20k30k40k50k60k443443 HTTPS3,3893389 RDP8,0808080 alt web51,544Client side, e.g.51544

Tap a shape with to see more.

443 HTTPS

Well-known ports 0–1023 belong to standard services.

3389 RDP

Registered ports are assigned to specific applications.

8080 alt web

A popular choice for test and proxy web servers.

Client side, e.g. 51544

Your browser picks a temporary (ephemeral) port for each connection.

Step 13 of 13Practice · Choose one

You try to connect to a server on port 8080 and immediately get "connection refused". What is the most likely explanation?