Code Katas
Focused, repeatable coding exercises designed to build fluency in socket programming and network reasoning. Complete each kata multiple times until the setup feels automatic. Keep a per-kata mistake log next to the code.
Kata 1: Concurrent Echo Server (Threaded)
Time limit: 30 minutes
Goal: Write a TCP echo server that serves many clients concurrently using one thread per connection.
Setup:
- Listen on port 9000.
- For each accepted connection, spawn a detached thread.
- Echo every byte back until the client closes.
- Gracefully handle partial
recvand EINTR.
Repeat until: You can type the skeleton (socket, setsockopt, bind, listen, accept, pthread_create, pthread_detach) from memory without a reference.
Kata 2: Concurrent Echo Server (Event-Driven)
Time limit: 45 minutes
Goal: Rewrite Kata 1 using epoll (Linux), kqueue (macOS/BSD), or select (portable fallback).
Setup:
- Make the listening socket non-blocking.
- Register it with the event loop.
- On
accept, add the new fd as non-blocking. - On readable fd, drain until
EAGAIN, then write back.
Repeat until: You can write this without deadlocking the loop on a slow client and you can explain why partial writes need to be buffered.
Kata 3: Minimal HTTP/1.1 Client
Time limit: 30 minutes
Goal: Write a client that sends GET /path HTTP/1.1 with Host and Connection: close, reads the full response, and prints the status line plus body.
Setup:
- Use
getaddrinfoto resolve hostnames. - Parse the status line and headers.
- Read exactly
Content-Lengthbytes of body (ignore chunked encoding for this kata).
Repeat until: Your client correctly handles 200, 301 (even if you don't follow it), and 404 responses.
Kata 4: Minimal HTTP/1.1 Server
Time limit: 45 minutes
Goal: Write a server that accepts HTTP/1.1 requests and responds with either 200 OK (for GET /) or 404 Not Found.
Setup:
- Parse the request line.
- Read headers until the blank line.
- Write a well-formed response with
Content-Type,Content-Length, andConnection: close. - Validate by hitting it with
curl -v.
Repeat until: Your server answers correctly even when the client's request arrives in two or three separate recv calls.
Kata 5: Capture and Annotate a TCP 3-Way Handshake
Time limit: 20 minutes
Goal: Produce a .pcap capture of a single TCP connection and annotate every packet by hand.
Setup:
- Run
nc -l 12345andnc localhost 12345. - Capture with
sudo tcpdump -i lo -w handshake.pcap "port 12345". - Open in Wireshark.
Annotation checklist:
- Packet 1:
SYN, seq, flags. - Packet 2:
SYN-ACK, seq, ack, flags. - Packet 3:
ACK, no payload. - First data segment, if any.
-
FINfrom one side. -
FIN-ACKand finalACK.
Repeat until: You can produce the capture in under two minutes and label the three handshake packets without looking up state-machine notes.
Kata 6: TCP vs UDP Throughput and Loss Tolerance
Time limit: 60 minutes
Goal: Measure how TCP and UDP differ under induced packet loss.
Setup:
- Use
iperf3 -sas a server on one machine (or a VM) andiperf3 -c ...as a client. - Run a baseline test in TCP and in UDP (with
-u -b 100M). - Use
tc qdisc add dev lo root netem loss 1%to inject loss (Linux) and rerun. - Increase loss to 5% and 10%.
Repeat until: You can explain, from the data, where TCP's goodput collapses and at what point UDP becomes the better choice -- and what "better" even means for each workload.
Completion Standard
- Can implement Katas 1 and 2 in one sitting each.
- Can run Kata 5 and label every packet without reference.
- Can explain the TCP-vs-UDP throughput tradeoff you measured in Kata 6 in one paragraph.
- Have written at least one mistake per kata in a log (
forgot htons,blocked on slow client in event loop,forgot Content-Length, etc.).