Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
TCP — The Reliable Stream  ›  Lesson

The TCP Connection

Connection 12 minTCP opens a connection with a three-way handshake before any data flows, identifies it by two addresses and two ports, and keeps state for it on the server until it is closed.

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
  1. SYN — "I would like to connect. My bytes will be numbered from 4000."
  2. SYN-ACK — "Agreed. Mine start at 7000, and I expect your 4001 next."
  3. 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."

A player joins the lobbyStep 1 of 4
  1. 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.

quick drill

In what order do the three messages of a TCP handshake travel?

quick drill

A client's SYN carries seq=500. What ack number does the server's SYN-ACK carry?

quick drill

One laptop opens two TCP connections to the lobby on port 443. What tells the two connections apart?

quick drill

The round-trip time to the lobby is 40 ms. How soon, at the earliest, can a brand-new connection send its first request?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhat do idle players cost?

Explain why Manja Arena's lobby server must care how many TCP connections are open, even when most players are sitting idle in menus, and why reconnecting over and over is expensive too.