The TCP Connection
Before a Manja Arena match starts, players wait in the lobby: logging in, browsing open matches, chatting with friends. Every one of those messages must arrive, in order, and nobody minds a few extra milliseconds, so the lobby server speaks TCP. TCP gives two programs a reliable, two-way stream of bytes, and it opens every stream with a short ritual.
The rule
Three messages open a connection; every open connection costs memory.
TCP rides on IP, which carries individual packets from one IP address to another; the IP section of this course looks underneath. A port, a number from 0 to 65535, picks which program on a machine receives the data. The lobby listens on port 443. The player's operating system picks a temporary ephemeral port for its own end, such as 50311.
A connection is identified by four values together: client address, client port, server address, server port. Change any one of them and it is a different connection.
Opening a connection takes the three-way handshake:
player :50311 ── SYN seq=4000 ────────────▶ lobby :443
player :50311 ◀─ SYN-ACK seq=7000 ack=4001 ── lobby :443
player :50311 ── ACK ack=7001 ────────────▶ lobby :443- SYN — "I would like to connect. My bytes will be numbered from 4000."
- SYN-ACK — "Agreed. Mine start at 7000, and I expect your 4001 next."
- ACK — "Got it. I expect your 7001 next."
Each side chooses its own starting sequence number and confirms the other's. The client can send data only once the SYN-ACK has arrived, so a new connection spends one full round trip before the first request leaves.
Closing is orderly too. Each side sends a FIN when it has nothing more to send, and the other side acknowledges it. The side that closed first then waits in a state called TIME_WAIT for a while, so that stray packets from the old connection cannot be mistaken for a new one.
Meanwhile, every open connection costs the server something: kernel memory for its state, a send buffer and a receive buffer, and a descriptor in the server process (the socket section covers those). A lobby with 50,000 players idling in menus is holding 50,000 of them.
The shortcut
Read any handshake with one rule: each ack number is the other side's sequence number plus one. It means "I have everything before this; send me this byte next."
SYN
The player's machine picks port 50311 and a starting number, 4000, and sends SYN seq=4000 to the lobby on port 443.
Drills
Answer fast — trust the rule.
In what order do the three messages of a TCP handshake travel?
A client's SYN carries seq=500. What ack number does the server's SYN-ACK carry?
One laptop opens two TCP connections to the lobby on port 443. What tells the two connections apart?
The round-trip time to the lobby is 40 ms. How soon, at the earliest, can a brand-new connection send its first request?
50,000 players sit idle in the lobby, each with an open TCP connection. What do those silent connections still cost the server?
Remember
- Three messages open a connection (SYN, SYN-ACK, ACK); every open connection costs memory.
- Each ack number is the other side's sequence number plus one.
- Trap: forgetting the handshake's round trip, and the cost of thousands of idle connections.
