Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    Sockets: How a Program Uses the Network

    "Address already in use" when starting a server — another process is already listening on that port.

    Chapter 2 of 125 min read

    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):

    Output
      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 caller

    Client side (making a call):

    Output
      socket()   get a phone
      connect()  dial 13.235.10.20:443 — the kernel picks an ephemeral port for us

    After 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 toWho can connect
    127.0.0.1 (loopback)only programs on the same machine
    0.0.0.0anyone 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):

    Terminal
    nc -l 9000              # listen on port 9000

    Terminal 2 (a client):

    Terminal
    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 :8000 and 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 questions
    1

    What is a socket, from the kernel's point of view?

    Show answer

    A kernel object for sending/receiving network data, given to the program as a file descriptor.

    2

    List the four server-side socket calls in order.

    Show answer

    socket, bind, listen, accept.

    3

    Why does accept() return a new socket?

    Show answer

    The listening socket keeps taking new callers; each accepted caller gets its own socket for its own conversation.

    4

    Your server works on your laptop but not from another machine. It's bound to 127.0.0.1. Why?

    Show answer

    Loopback only accepts connections from the same machine; bind to 0.0.0.0 instead.