|
| 1 | +# TCP/IP and Socket Connections |
| 2 | + |
| 3 | +## The TCP/IP Model |
| 4 | + |
| 5 | +TCP/IP is the foundational framework that defines how data travels across the internet. |
| 6 | +It has 4 layers (simpler than the 7-layer OSI model): |
| 7 | + |
| 8 | +| Layer | Name | What it does | Examples | |
| 9 | +|-------|------|-------------|---------| |
| 10 | +| 4 | Application | User-facing protocols | HTTP, DNS, SSH, FTP | |
| 11 | +| 3 | Transport | End-to-end communication | TCP, UDP | |
| 12 | +| 2 | Internet | Addressing and routing | IP | |
| 13 | +| 1 | Network Access | Physical transmission | Ethernet, WiFi | |
| 14 | + |
| 15 | +Each layer handles its own responsibility and passes data to the layer above/below. |
| 16 | +Your FastAPI app lives at Layer 4. It doesn't know or care about Layers 1-2. |
| 17 | + |
| 18 | +## TCP vs UDP |
| 19 | + |
| 20 | +| Feature | TCP | UDP | |
| 21 | +|---------|-----|-----| |
| 22 | +| Full name | Transmission Control Protocol | User Datagram Protocol | |
| 23 | +| Reliability | Guaranteed delivery | Best-effort, no guarantee | |
| 24 | +| Order | Packets arrive in order | Order not guaranteed | |
| 25 | +| Speed | Slower (overhead for reliability) | Faster (no overhead) | |
| 26 | +| Connection | Connection-oriented (handshake) | Connectionless | |
| 27 | +| Use cases | HTTP, SSH, email, file transfer | Live video, DNS, gaming, VoIP | |
| 28 | + |
| 29 | +Rule of thumb: if losing data is unacceptable → TCP. If speed matters more → UDP. |
| 30 | + |
| 31 | +## The TCP 3-Way Handshake |
| 32 | + |
| 33 | +Before any data is sent over TCP, a connection must be established. |
| 34 | +This takes exactly 3 messages: |
| 35 | + |
| 36 | +``` |
| 37 | +Client Server |
| 38 | + | | |
| 39 | + |-------- SYN ----------------->| "I want to connect, my seq=X" |
| 40 | + | | |
| 41 | + |<------- SYN-ACK --------------| "OK, I got X, my seq=Y, ack=X+1" |
| 42 | + | | |
| 43 | + |-------- ACK ----------------->| "Got it, ack=Y+1, let's go" |
| 44 | + | | |
| 45 | + |====== Data Transfer ==========| |
| 46 | +``` |
| 47 | + |
| 48 | +- **SYN** (Synchronize): client initiates |
| 49 | +- **SYN-ACK**: server acknowledges and responds |
| 50 | +- **ACK** (Acknowledge): client confirms - connection open |
| 51 | + |
| 52 | +Only after all 3 steps does actual data begin flowing. |
| 53 | + |
| 54 | +## What is a Socket? |
| 55 | + |
| 56 | +A Socket is the software abstraction that represents one endpoint of a network connection. |
| 57 | + |
| 58 | +``` |
| 59 | +Socket = IP Address + Port + Protocol |
| 60 | +``` |
| 61 | + |
| 62 | +Examples: |
| 63 | +- Server socket: `0.0.0.0:8000 (TCP)` - your FastAPI app listening for connections |
| 64 | +- Client socket: `192.168.1.5:52341 (TCP)` - your browser's temporary connection port |
| 65 | + |
| 66 | +When you run `uvicorn main:app --host 0.0.0.0 --port 8000`, you are: |
| 67 | +1. Creating a socket |
| 68 | +2. Binding it to port 8000 |
| 69 | +3. Telling the OS: "give me everything arriving on port 8000" |
| 70 | + |
| 71 | +## Full Flow: Browser to Server |
| 72 | + |
| 73 | +``` |
| 74 | +1. DNS lookup: google.com → 142.250.74.46 |
| 75 | +2. TCP Handshake: SYN → SYN-ACK → ACK (port 443) |
| 76 | +3. TLS Handshake: agree on encryption keys |
| 77 | +4. HTTP GET / sent (encrypted) |
| 78 | +5. HTTP 200 OK response received (encrypted) |
| 79 | +6. Browser renders HTML |
| 80 | +``` |
| 81 | + |
| 82 | +## Key Concepts |
| 83 | + |
| 84 | +- TCP/IP = 4-layer model. Your app lives at Application layer. |
| 85 | +- TCP = reliable, ordered, connection-oriented (3-way handshake) |
| 86 | +- UDP = fast, unreliable, connectionless |
| 87 | +- Socket = IP + Port + Protocol |
| 88 | +- 3-Way Handshake = SYN, SYN-ACK, ACK - happens before every TCP connection |
0 commit comments