The Big Picture: Two Ways to Send a Message
IP (Section 2) can carry a packet from one computer to another. But IP makes no promises. A packet can be lost, copied, delayed or arrive out of order. IP is like dropping letters in a postbox: usually they arrive, but nobody guarantees it.
On top of IP sit two transport protocols. They make the delivery useful for programs. They are two different "postal services":
| Service | Real-life comparison | Name |
|---|---|---|
| Registered mail with a tracking number | You get a receipt for each parcel; lost ones are sent again; parcels are reassembled in order | TCP |
| A postcard | Drop it in the box and hope. Cheap and quick; no receipt | UDP |
Both also add port numbers, so the data reaches the right program on the PC (Section 7). The transport layer is where the segment or datagram of Section 1.5 is made.
6.1 TCP (Reliable, Connection-Oriented)
What it is: TCP (Transmission Control Protocol) gives programs a reliable, ordered stream of bytes. "Reliable" means every byte arrives, or the program is told it failed. "Ordered" means bytes arrive in the order they were sent. "Connection-oriented" means the two ends first set up a connection and later close it.
How TCP keeps its promises:
| Feature | What it does | Everyday comparison |
|---|---|---|
| Connection setup | A three-way handshake (6.3) before any data | A phone call: "Hello?" "Hello!" "Hi, it's me" |
| Sequence numbers | Every byte (or chunk) is numbered | Page numbers in a book |
| Acknowledgements (ACKs) | The receiver says "I got everything up to number N" | A receipt |
| Retransmission | If no ACK comes in time, the sender sends again | Re-sending a lost parcel |
| Reordering | Pieces that arrive out of order are put right | Sorting the pages |
| Checksum | Detects damaged data | A seal on the envelope |
| Flow control | The receiver says how much more it can take (the window) | "Slow down, my hands are full" |
| Congestion control | The sender slows down when the network looks overloaded | Drivers easing off in a traffic jam |
| Closing | Both sides say goodbye (FIN messages) | "Bye." "Bye." |
How data and ACKs flow:
SENDER RECEIVER
segment: bytes 1-1000 ───────────────────────────►
segment: bytes 1001-2000 ───────────X (lost on the way)
segment: bytes 2001-3000 ───────────────────────────►
◄─────── ACK 1001 ("got up to 1000, send 1001 next")
◄─────── ACK 1001 (still waiting for 1001)
(no ACK for 1001 in time, so send it again)
segment: bytes 1001-2000 ───────────────────────────►
◄─────── ACK 3001 ("got everything up to 3000")
The TCP header is added to the data (Section 1.5). The fields you should know:
| Field | Meaning |
|---|---|
| Source port, Destination port | Which program sends, which receives (16 bits each, Section 7) |
| Sequence number | Position of this data in the stream |
| Acknowledgement number | The next byte the sender of this segment expects |
| Flags | Single-bit switches such as SYN, ACK, FIN, RST (see below) |
| Window size | How much data the sender of this segment can currently accept |
| Checksum | Error check |
| Flag | Meaning |
|---|---|
| SYN | "Synchronize": start a connection and share starting sequence numbers |
| ACK | "Acknowledge": the acknowledgement number is valid |
| FIN | "Finish": I have no more data to send |
| RST | "Reset": abort this connection now (for example, no program listens on that port) |
The cost of reliability. TCP costs extra messages (the handshake, the ACKs) and, when something is lost, waiting. This is a good trade for things where every byte matters.
Used for: web pages (HTTP and HTTPS), email, file transfer, remote login (SSH, RDP), file sharing (SMB) and most things where correctness beats speed.
6.2 UDP (Unreliable, Connectionless, Fast)
What it is: UDP (User Datagram Protocol) is the postcard. The program hands UDP a message, UDP adds a tiny header, and IP sends it. That's all. There is no handshake, no acknowledgement, no retransmission and no ordering.
The UDP header is only 8 bytes:
┌─────────────┬─────────────┬──────────┬──────────┐
│ Source port │ Dest port │ Length │ Checksum │
│ (2 bytes) │ (2 bytes) │ (2 bytes)│ (2 bytes)│
└─────────────┴─────────────┴──────────┴──────────┘
Compare this with TCP's header, which is at least 20 bytes plus the extra messages for the connection.
| TCP | UDP | |
|---|---|---|
| Connection | Yes (handshake first) | None |
| Delivery guaranteed | Yes, with retransmission | No |
| Order kept | Yes | No |
| Speed and overhead | Slower, more overhead | Fast, very small header |
| Message style | A continuous stream of bytes | Separate datagrams (each message stands alone) |
| Header size | 20 bytes or more | 8 bytes |
| Typical use | Web, email, file transfer | DNS, DHCP, voice and video calls, online games, streaming |
Why use something that can lose data?
- When late data is useless data. In a voice call, a word that arrives two seconds late is worse than a tiny gap. The program just skips it and carries on. Waiting for a resend would make the call stutter.
- When the message is tiny. A DNS question (Section 4) fits in one datagram. A handshake would cost more than the question itself.
- When the program wants control. Some programs build their own reliability on top of UDP. A modern example is QUIC, the protocol behind newer web traffic (HTTP/3), which adds its own reliability and encryption on top of UDP.
If UDP loses something, who cares? The program. A DNS client simply asks again if no answer arrives. A game keeps playing with the newest update.
UDP and broadcast. Because UDP has no connection, it can send to a whole network at once. DHCP (Section 5) uses this for its first message.
6.3 The TCP Three-Way Handshake
What it is: Before sending data, TCP sets up a connection with three messages. The aim is that both sides agree they are talking, and agree on the starting sequence numbers.
CLIENT (your browser) SERVER (web server)
(port 50123) (port 443)
1. SYN "I want to connect. My first sequence number is 1000."
─────────────────────────────────────────────────────────►
2. SYN-ACK "OK, I hear you (I expect 1001). My first number is 5000."
◄─────────────────────────────────────────────────────────
3. ACK "Got it (I expect 5001). Connection open."
─────────────────────────────────────────────────────────►
Now data can flow in both directions.
| Step | Flags | Meaning |
|---|---|---|
| 1 | SYN | The client asks to open a connection and says where its numbering starts |
| 2 | SYN + ACK | The server agrees, confirms the client's number and says where its own numbering starts |
| 3 | ACK | The client confirms the server's number. Both sides now know the connection is open |
(The numbers in the picture are examples. Real starting numbers are chosen unpredictably, which makes the connection harder to hijack.)
What if the server has no program listening on that port? It answers the SYN with a RST ("reset"). Your program then reports "connection refused". If nobody answers at all (for example, a firewall silently drops it), the SYN is repeated a few times and then the program reports a timeout. These two failures look different, and that difference helps when troubleshooting (see Section 10).
Closing a connection. Each side sends a FIN (finish) and the other answers with an ACK. Often this takes four messages, since each direction is closed separately.
What you can see on your own PC: In netstat -ano (Section 7), a TCP connection has a State column:
| State | Meaning |
|---|---|
| LISTENING | A program is waiting for incoming connections |
| ESTABLISHED | The handshake is done; the connection is open |
| TIME_WAIT | A connection was just closed and is waiting a short while to catch stray packets |
| SYN_SENT | Your PC sent a SYN and is waiting for the SYN-ACK |
Test a TCP connection safely (read-only):
Test-NetConnection example.com -Port 443
This command tries the TCP handshake to port 443 of example.com and reports TcpTestSucceeded : True when it works. Use it only on your own devices or public sites.
Summary
| Topic | Remember this |
|---|---|
| Transport layer | Delivers data between programs using port numbers |
| TCP | Connection first; sequence numbers, ACKs, retransmission, flow and congestion control; reliable and ordered |
| UDP | No connection, no ACKs; 8-byte header; fast; used for DNS, DHCP, calls, games, streaming |
| Handshake | SYN, then SYN-ACK, then ACK |
| Flags | SYN start, ACK acknowledge, FIN finish, RST abort |
| Failure signs | RST means refused (nothing listening); silence means timeout (blocked or down) |
| Choosing | Correctness matters: TCP. Lateness matters more than loss: UDP |