Master the fundamental concepts of tcp/ip from scratch through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksTCP is full-duplex: each direction closes independently. A normal teardown takes four segments. For example, the active closer on port 8080 sends FIN, the passive side ACKs, then sends its own FIN, and the active side sends the final ACK.
Active closer: ESTABLISHED -> FIN_WAIT_1 -> FIN_WAIT_2 -> TIME_WAIT -> CLOSED
Passive closer: ESTABLISHED -> CLOSE_WAIT -> LAST_ACK -> CLOSED
The FIN flag consumes one sequence number, just like SYN. After the final ACK, the active closer sits in TIME_WAIT for 2 x MSL (typically 60-120 seconds) to catch retransmitted FINs and reject stale segments from old connections.
RST aborts immediately with no graceful shutdown. Common triggers:
9999 with no listener)This task requires you to simulate the FIN/ACK sequence with state transitions. TIME_WAIT accumulation from this close pattern is a well-known production incident for high-throughput servers like nginx, which is why SO_REUSEADDR exists. Operators tune net.ipv4.tcp_tw_reuse and related sysctls because thousands of connections to port 443 can leave the server holding sockets in TIME_WAIT for 60-120 seconds each.
Given a close-segment trace, run the active closer's TCP teardown state machine (RFC 9293). The FIN consumes one sequence number, TIME_WAIT lasts 2*MSL, and RST aborts without any graceful exchange.
Input (stdin): one line: msl initial_seq N (msl in seconds). Then N segment events: FIN, ACK, FIN+ACK, RST. The first FIN (while ESTABLISHED) is ours and consumes one sequence number; later FIN segments are the peer's.
Output (stdout):
Transitions: ESTABLISHED + FIN -> FIN_WAIT_1; FIN_WAIT_1 + ACK -> FIN_WAIT_2; FIN_WAIT_1 + FIN -> CLOSING (simultaneous close); FIN_WAIT_1 + FIN+ACK -> TIME_WAIT; FIN_WAIT_2 + FIN -> TIME_WAIT; CLOSING + ACK -> TIME_WAIT; RST -> CLOSED from any state (abort, no TIME_WAIT).