skipnothing/HTTP & Networking

TCP Handshake

connect() looks instant in your code, but the socket sits quiet for one round trip first, while two machines trade three small messages to agree on where each will start counting bytes.

  • ▸Each side counts bytes from its own random start, and neither can guess the other's, so it must be told.
  • ▸Two messages can never be enough: one endpoint reaches connected a full step ahead of the other.
  • ▸Opening a connection costs one round trip before any data, which is the whole reason pooling exists.
01

Two random starting numbers

Every byte TCP sends gets a number, and the count does not begin at zero. Each side rolls a random starting value the moment it opens the socket, its initial sequence number, and announces it to the other. The far side has no way to guess that value; it has to be told. Until both origins are on the table, a raw number like 4183262244 means nothing on its own: is it the first byte or the two-millionth? Every message that follows exists to trade these two independent starting points and confirm each one was heard. The origins are the whole reason the exchange has to happen at all.

ROLL EACH SIDE'S ORIGIN

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 23+ more topics in HTTP & Networking.

Sign up, it's freeSee the full HTTP & Networking