🌐 HTTP Fundamentals
When you type a URL and hit Enter, this is what happens. Click any step to learn more.
🔌 TCP & Sockets
HTTP rides on top of TCP (Transmission Control Protocol). While HTTP defines the message format, TCP handles the actual delivery — ensuring bytes arrive in order, retransmitting lost packets, and managing connections.
A socket is the programming interface to TCP. When your Flask app listens on port 5000, it's creating a socket that waits for incoming TCP connections.
When you run flask run --port 5000, here's what happens:
# Your app does (simplified):
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 5000)) # Claim port 5000
sock.listen() # Start accepting connections
while True:
client, addr = sock.accept() # Wait for a connection
data = client.recv(1024) # Read the HTTP request
client.send(b'HTTP/1.1 200 OK\r\n\r\nHello')
client.close()
The bind() call claims the port. If another process already has it, you get the dreaded error:
OSError: [Errno 98] Address already in use
This usually means:
- Another instance of your app is running
- The previous instance crashed but the OS hasn't released the port yet
- Some other service is using that port
Connection Lifecycle
TCP connections go through a handshake before data can flow:
Client Server
| |
|-------- SYN ----------->| "I want to connect"
|<------ SYN-ACK ---------| "OK, I acknowledge"
|-------- ACK ----------->| "Great, let's go"
| |
|====== DATA FLOWS =======|
| |
|-------- FIN ----------->| "I'm done"
|<------ FIN-ACK ---------| "OK, me too"
1. What is a port?
2. "Address already in use" usually means:
🚧 Coming Soon
This section is still being written. Check back soon!