The Plain Picture#
A phone line in a flat. To receive calls, a family must (1) get a phone, (2) connect it to their flat number, (3) switch the ringer on, and (4) pick up when it rings. To make a call, you get a phone and dial.
The Real Names#
A socket is the kernel object a program uses to send and receive network data — the phone. To the
program it's just a file descriptor (Operating Systems Ch 10), read and written with the
same read/write system calls as a file.
Server side (receiving calls):
socket() get a phone
bind() attach it to an address and port, e.g. 0.0.0.0:8000
listen() switch the ringer on — the kernel starts accepting connections for us
accept() pick up — returns a NEW socket just for this one callerClient side (making a call):
socket() get a phone
connect() dial 13.235.10.20:443 — the kernel picks an ephemeral port for usAfter that, both sides read() and write() bytes.
Under the Hood#
One listening socket, many connection sockets:
StudyBuddy server process ┌──────────────────────────────────────────────────────────────┐ │ listening socket 0.0.0.0:8000 (only accepts new calls) │ │ │ │ │ ├── accept() → connection socket ↔ client A:52341 │ │ ├── accept() → connection socket ↔ client B:61022 │ │ └── accept() → connection socket ↔ client C:49877 │ └──────────────────────────────────────────────────────────────┘
With blocking I/O, a server needs a thread per connection socket; with an event loop, one thread watches
all of them with epoll/kqueue (Operating Systems Ch 11).
Bind address: 127.0.0.1 vs 0.0.0.0#
Which address you bind to decides who can connect:
| Bind to | Who can connect |
|---|---|
127.0.0.1 (loopback) | only programs on the same machine |
0.0.0.0 | anyone who can reach any of this machine's addresses |
192.168.1.37 (one specific address) | only via that address |
This is the 0.0.0.0 "server meaning" promised in Networking 1: Addresses and Routing Ch 6.
Classic bug: you run StudyBuddy's backend inside a Docker container bound to 127.0.0.1:8000. From your
laptop, nothing connects — because 127.0.0.1 inside the container means the container itself. Binding to
0.0.0.0:8000 fixes it. (Docker (coming soon).)
Try it with no programming — nc (netcat)#
Terminal 1 (a server):
nc -l 9000 # listen on port 9000Terminal 2 (a client):
nc localhost 9000 # connect, then type "hello" and press Enter"hello" appears in terminal 1. You just used a TCP socket by hand. In a third terminal, run
lsof -i :9000 to see both ends of the connection with their ports.
Gotchas
"Address already in use" when starting a server = another process is already listening on that port. Find it with
lsof -i :8000and stop it.A socket is a file descriptor, so leaked sockets count toward the "Too many open files" limit (Operating Systems Ch 10).
Check yourself
4 questionsWhat is a socket, from the kernel's point of view?
Show answerHide answer
A kernel object for sending/receiving network data, given to the program as a file descriptor.
List the four server-side socket calls in order.
Show answerHide answer
socket, bind, listen, accept.
Why does accept() return a new socket?
Show answerHide answer
The listening socket keeps taking new callers; each accepted caller gets its own socket for its own conversation.
Your server works on your laptop but not from another machine. It's bound to 127.0.0.1. Why?
Show answerHide answer
Loopback only accepts connections from the same machine; bind to 0.0.0.0 instead.