# TCP — Transmission Control Protocol

## What is TCP?
TCP is a **connection-oriented**, reliable transport layer protocol (Layer 4). It guarantees delivery, ordering, and error checking of data between two devices.

## The Three-Way Handshake
Before data is exchanged, TCP establishes a connection:

```
Client          Server
  |--- SYN ------->|   "I want to connect, my seq=X"
  |<-- SYN-ACK ----|   "OK, my seq=Y, ack=X+1"
  |--- ACK ------->|   "Acknowledged, ack=Y+1"
  |   [Connected]  |
```

1. **SYN** — Client sends a synchronize segment to initiate connection.
2. **SYN-ACK** — Server acknowledges and sends its own SYN.
3. **ACK** — Client acknowledges the server's SYN. Connection is established.

## Session Termination (Four-Way)
```
Client          Server
  |--- FIN ------->|
  |<-- ACK --------|
  |<-- FIN --------|
  |--- ACK ------->|
  |   [Closed]     |
```

## TCP vs UDP

| Feature | TCP | UDP |
|---------|-----|-----|
| Connection | Connection-oriented | Connectionless |
| Reliability | Guaranteed delivery | No guarantee |
| Order | In-order delivery | No ordering |
| Speed | Slower (overhead) | Faster |
| Use case | HTTP, DNS (zone transfer), FTP | DNS queries, streaming, VoIP |

## Observed in
- Activity 4.1.6 — UDP and TCP Port Numbers
- Activity 4.2.5 — TCP Session Establishment and Termination
- Activity 4.5.3 — Application and Transport Layer Protocols
