Cloud Explainers

How TCP Sockets Work

Networking9 min video

How TCP sockets work, on real machines: a pizza kitchen's order screen and its tablets, written in plain Python. We follow one socket from socket(), bind(), listen() and accept() to close(): a connection that succeeds before accept() is called, the three-way handshake and a refusal in tcpdump, one port serving four connections, two orders glued into one ticket because TCP carries bytes, not messages, and 'Address already in use' from TIME-WAIT.

Two orders, one ticket

You send two messages over a TCP socket, and you expect two to arrive. At a pizza kitchen, Ana's tablet sends two orders to the kitchen screen: 2 margherita, then 1 pepperoni. The screen prints one ticket, the two orders glued together. Nothing was lost and nothing crashed. So where did the line between the orders go? The answer is in how a socket really works.

Ana: kitchen and tablet
python3 kitchen.pykitchen screen: listening on port 9000
Ana: kitchen and tablet
python3 tablet.py "2 margherita" "1 pepperoni"connected: 10.48.0.21:55930 -> 10.48.0.10:9000
sent: 2 margherita
sent: 1 pepperoni
Ana: kitchen and tablet
python3 kitchen.pykitchen screen: listening on port 9000
tablet connected from ('10.48.0.21', 55930)
TICKET: 2 margherita1 pepperoni

One number, many lines

Every TCP connection runs between two addresses and two ports. The address finds the machine. The port finds one program on it: a number from 1 to 65535. Here is the kitchen's listening socket, as ss, a tool that lists a machine's sockets, prints it. LISTEN: waiting for calls. 0.0.0.0: every address this machine has. 9000: the port Ana chose. And the star: calls from any address and port.

The kitchen's listening socket, as ss prints it: diagram of LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*, LISTEN = waiting for calls, 0.0.0.0 = every address, :9000 = the port, 0.0.0.0:* = any caller, Address: the machine · Port: one program on it

A socket is one end of a conversation between two programs. Think of a phone line. The kitchen has one number everyone dials: in TCP, an address and a port, here port 9000. When a call comes in, the caller gets a line of their own, and the main number is free to ring again. TCP works the same way: one listening socket, the number, and one connected socket for every caller.

A phone line vs a TCP socket: comparison table of A phone vs TCP, Everyone dials, Each call gets, Then, Talking

Four calls to open the kitchen

Ana's kitchen program opens with four calls. socket makes a new socket: a phone with no number yet. bind gives it an address and a port: every address on this machine, port 9000. listen says: take calls now. And accept waits for a caller, then returns a brand new socket for that one tablet. The tablet makes a socket too, then needs one more call: connect, to the kitchen's address and port.

The kitchen's four calls, the tablet's one: diagram of socket(), bind(("0.0.0.0", 9000)), listen(), accept(), connect(("kitchen", 9000))

So here's a test. This version of the screen calls listen, then waits for Enter before it calls accept. ss lists the sockets on port 9000: one, listening. Now the tablet connects and sends 4 garlic knots. Guess first: does connect work before accept? Yes. Connected, and sent.

Ana: kitchen and tablet
python3 not_yet.pylistening on port 9000, not accepting yet (Enter accepts one tablet)
Ana: kitchen and tablet
ss -tan 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
Ana: kitchen and tablet
python3 tablet.py "4 garlic knots"connected: 10.48.0.21:55942 -> 10.48.0.10:9000
sent: 4 garlic knots

So who answered? The kernel did, the core of the operating system. ss shows the listening socket with one call waiting in its queue, and a connected socket already holding fourteen bytes: the order. When Ana presses Enter, accept just hands over a call that was answered seconds ago, order and all. Your program never does the handshake. The kernel does.

Ana at the kitchen
ss -tan 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 1 128 0.0.0.0:9000 0.0.0.0:*
ESTAB 14 0 10.48.0.10:9000 10.48.0.21:55942
Ana at the kitchen
python3 not_yet.pylistening on port 9000, not accepting yet (Enter accepts one tablet)
accepted ('10.48.0.21', 55942) - it had already sent: 4 garlic knots

The handshake on the wire

What does that handshake look like? First, with nothing listening: the screen is closed, and the tablet connects anyway. Connection refused. tcpdump on the kitchen prints every packet, each small chunk of data, crossing its network card. It shows two: an S, a SYN, which asks: can we talk? And straight back an R, a reset: nobody is listening on port 9000.

Ana: kitchen and tablet
python3 tablet.py "1 hawaiian"Traceback (most recent call last):
  File "/home/ana/tablet.py", line 6, in <module>
    tab.connect(("kitchen", 9000))
ConnectionRefusedError: [Errno 111] Connection refused
Ana: kitchen and tablet
sudo timeout 6 tcpdump -n -t -l -i eth0 tcp port 9000 2>/dev/null | cut -d, -f1IP 10.48.0.21.45192 > 10.48.0.10.9000: Flags [S]
IP 10.48.0.10.9000 > 10.48.0.21.45192: Flags [R.]

With the screen running, the same connect takes three packets. SYN from the tablet. SYN-ACK from the kitchen: yes, and can you hear me? ACK: yes, which tcpdump draws as a dot. That's the three-way handshake, and only then does connect return. The order goes in one packet marked P, for push, and the kitchen acknowledges it. When the tablet hangs up, each side sends an F, for finish, and the last ACK closes it.

Ana at the kitchen
sudo timeout 12 tcpdump -n -t -l -i eth0 tcp port 9000 2>/dev/null | cut -d, -f1IP 10.48.0.21.41176 > 10.48.0.10.9000: Flags [S]
IP 10.48.0.10.9000 > 10.48.0.21.41176: Flags [S.]
IP 10.48.0.21.41176 > 10.48.0.10.9000: Flags [.]
IP 10.48.0.21.41176 > 10.48.0.10.9000: Flags [P.]
IP 10.48.0.10.9000 > 10.48.0.21.41176: Flags [.]
IP 10.48.0.21.41176 > 10.48.0.10.9000: Flags [F.]
IP 10.48.0.10.9000 > 10.48.0.21.41176: Flags [F.]
IP 10.48.0.21.41176 > 10.48.0.10.9000: Flags [.]

Put together, that's the life of one connection. The kitchen listens. The tablet's connect sets off the handshake, and accept hands the kitchen its new socket. Then send on one side and recv on the other, as often as they like, in both directions. And close ends it. Your browser does exactly this for every website it opens, on port 443.

The life of one connection: diagram of Tablet, Kitchen, connect(), connected, sendall(order), send(reply), close()

One port, many tablets

A busy evening: three tablets, and one of them connected twice. The screen prints each connection and every order. On the kitchen, ss shows one listening socket, and four connected ones, all on port 9000.

Ana at the kitchen
python3 kitchen.pytablet connected from ('10.48.0.21', 41186)
TICKET: 1 lasagna
tablet connected from ('10.48.0.22', 51898)
tablet connected from ('10.48.0.22', 51906)
tablet connected from ('10.48.0.23', 53294)
TICKET: 2 tiramisu
TICKET: 1 cola
TICKET: 3 garlic bread
Ana at the kitchen
ss -tan 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
ESTAB 0 0 10.48.0.10:9000 10.48.0.23:53294
ESTAB 0 0 10.48.0.10:9000 10.48.0.21:41186
ESTAB 0 0 10.48.0.10:9000 10.48.0.22:51906
ESTAB 0 0 10.48.0.10:9000 10.48.0.22:51898

That needs one more piece in the program. recv waits until something arrives, so a program stuck reading tablet one can't hear tablet two. So for every socket accept returns, Ana's screen starts a thread: one worker per line. Big servers do the same job with tools that watch thousands of sockets at once.

A worker for every connected socket: diagram of accept(), thread 1, thread 2, thread 3

And each tablet's socket knows both ends of its line. Tablet one prints them as it connects. Its own address and port, 41186, a port its system picked. And the kitchen's, 10.48.0.10, port 9000.

Ana: kitchen and tablet
python3 tablet.py "1 lasagna"connected: 10.48.0.21:41186 -> 10.48.0.10:9000
sent: 1 lasagna

So how does the kitchen tell them apart, if they all use port 9000? By four numbers together: the kitchen's address and port, and the tablet's address and port. The tablet's port is picked by the tablet's system, new for each connection, so tablet two's two connections differ only there: 51898 and 51906. Each set of four is one connected socket, one line. The listening socket only answers the number.

One port, a socket for every connection: diagram of 10.48.0.10:9000 <-> 10.48.0.22:51898, 10.48.0.10 = kitchen address, :9000 = kitchen port, 10.48.0.22 = tablet address, :51898 = tablet port, Tablet 2's other connection: port 51906

Where the line between orders went

Now, the glued ticket. TCP doesn't carry messages. It carries a stream of bytes, like water through a pipe. The tablet's two sends poured 23 bytes in, one after the other. recv takes whatever has arrived, up to 1024 bytes, and this screen reads once a second, so both orders were waiting: one read, one ticket. Nothing in the stream says where one order ends.

TCP carries bytes, not messages: diagram of 2 margherita1 pepperoni, 2 margherita = first send, 1 pepperoni = second send, One recv(1024): all 23 bytes, no border between them

Python's own socket guide says it plainly. send and recv work on the network's buffers, the memory where bytes wait. They may not even hand over all the bytes at once, so a message must have a fixed length, be delimited, say how long it is, or end by closing the connection. Ana picks the simplest: a delimiter, the end of a line.

Where the line between orders went: browser screenshot of send and recv work on buffers, not always all the bytes at once, so mark where a message ends

On the tablet, one line changes: each order now ends with a newline character, backslash n, before encode turns the text into bytes.

One order per line: code change of tablet.py, tab.sendall((order + "\n").encode()) (ends in \n)

On the kitchen, the screen stops taking whatever has arrived, and reads one line at a time instead, however the bytes came in. The tablet marks where an order ends, and the kitchen reads up to that mark.

The kitchen reads lines, not whatever arrived: comparison table of Before vs After, Reads, Gets, Two orders

Same two orders. 2 margherita, 1 pepperoni. Two tickets. The line between them was never in the stream, so the programs have to put it there.

Ana: kitchen and tablet
python3 tablet.py "2 margherita" "1 pepperoni"connected: 10.48.0.21:37822 -> 10.48.0.10:9000
sent: 2 margherita
sent: 1 pepperoni
Ana: kitchen and tablet
python3 kitchen.pykitchen screen: listening on port 9000
tablet connected from ('10.48.0.21', 37822)
TICKET: 2 margherita
TICKET: 1 pepperoni

Hanging up, and the minute after

Hanging up travels through the same stream. When a tablet closes its socket, the kitchen's recv returns zero bytes, an empty read, which is how the screen knew to print: tablet hung up. Then it's closing time: Ana stops the screen while three tablets are still connected, and the tablet hears the same thing from the other side: the kitchen closed the connection.

Ana: kitchen and tablet
python3 kitchen.pykitchen screen: listening on port 9000
tablet connected from ('10.48.0.21', 55930)
TICKET: 2 margherita1 pepperoni
tablet hung up: ('10.48.0.21', 55930)
Ana: kitchen and tablet
python3 tablet.py "2 margherita" "1 pepperoni"connected: 10.48.0.21:37822 -> 10.48.0.10:9000
sent: 2 margherita
sent: 1 pepperoni
the kitchen closed the connection

She changes her mind and starts the screen again, right away. Guess first: what happens? Address already in use. Nothing else is running. ss shows why: the three old connections are in TIME-WAIT, on a 60-second timer, and while they wait, the kernel won't let a new socket bind to port 9000. The side that hangs up first keeps its end for that minute, so a late packet from the old call can't land in a new one. Here, they were gone 61 seconds after the stop.

Ana at the kitchen
python3 kitchen.pyTraceback (most recent call last):
  File "/home/ana/kitchen.py", line 15, in <module>
    srv.bind(("0.0.0.0", 9000))                               # 2. an address and a port
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 98] Address already in use
Ana at the kitchen
ss -tano 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
TIME-WAIT 0 0 10.48.0.10:9000 10.48.0.23:53296 timer:(timewait,59sec,0)
TIME-WAIT 0 0 10.48.0.10:9000 10.48.0.22:51920 timer:(timewait,59sec,0)
TIME-WAIT 0 0 10.48.0.10:9000 10.48.0.21:37822 timer:(timewait,59sec,0)

The usual fix is one line before bind, which grep finds here: the SO_REUSEADDR option, reuse the address. It tells the kernel to let the new listening socket share the port with old connections in TIME-WAIT. One catch: it helps from the next restart, because the waiting sockets keep the options of the program that made them. So this time, with the fixed screen running, Ana stops it with two tablets connected, and starts it straight away: listening, right next to the two in TIME-WAIT.

Ana at the kitchen
grep -n SO_REUSEADDR kitchen.py15:srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)    # reuse the port right after a restart
Ana at the kitchen
python3 kitchen.pykitchen screen: listening on port 9000
tablet connected from ('10.48.0.21', 41396)
TICKET: 1 tiramisu
tablet connected from ('10.48.0.22', 38692)
TICKET: 2 lemonade
kitchen screen closing
Ana at the kitchen
ss -tan 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:9000 0.0.0.0:*
TIME-WAIT 0 0 10.48.0.10:9000 10.48.0.22:38692
TIME-WAIT 0 0 10.48.0.10:9000 10.48.0.21:41396

Recap

So: a socket is one end of a connection. The kernel does the handshake. One port serves every tablet, told apart by four numbers. TCP is a stream, so mark where messages end. And hanging up first leaves a minute of TIME-WAIT.

TCP sockets in five lines: diagram of A socket is one end of a connection, The kernel does the handshake, One port, many sockets, A stream of bytes, not messages, Hanging up first: TIME-WAIT

If the glued ticket makes sense now, a like helps YouTube show this to more beginners, and subscribing brings more networking, shown on real machines.

Every connection, ticket and packet in this video came from real machines, and every command and the lab files are linked in the description. Thanks for watching Cloud Explainers.

Sources

Demos: four containers on our own Mac standing in for a pizza kitchen's order screen and tablets; the kitchen and its orders are made up. Every connection, ticket and packet was real. Watch the full video on YouTube.

More videos

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index