Electronics & Telecommunication (University) is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Connection establishment and release

Connection establishment and release explain how two endpoints set up and tear down a reliable connection (sequence numbers, flags, timers like RTO and TIME-WAIT) used for TCP-style connection-oriented communication.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two reasons an engineer needs this: (1) To implement or debug a reliable, connection-oriented transport (for example TCP) used by web servers, file transfer and many networked applications. (2) To dimension timers and detect faults correctly so connections are robust (avoid spurious retransmits, resource leaks, or data duplication).

Key ideas

Start from first principles. Define terms before use.

  • Connection-oriented communication: a mode where two endpoints first create a logical session (a "connection") before exchanging application data; the connection provides state (sequence numbers, windows) that enables reliability, ordered delivery and flow control.

  • Segment: one transport-layer message unit (for TCP this is a TCP segment). It carries header fields (sequence number, acknowledgement number, flags, window) and payload.

  • Sequence number (Seq): a number that uniquely labels bytes in the send-bytes stream. In TCP this is a 32-bit unsigned integer that increases by the number of data bytes sent. Sequence numbers allow reliable, ordered delivery and duplicate suppression.

  • Acknowledgement number (Ack): the next sequence number the receiver expects; acknowledges receipt of all prior bytes.

  • SYN (synchronize) flag: used to start a connection and establish initial sequence numbers. A SYN consumes one sequence number.

  • ACK flag: indicates the Ack field is valid.

  • FIN (finish) flag: used to close the sender's side of the byte stream (a half-close). A FIN also consumes one sequence number.

  • RST (reset) flag: aborts a connection immediately; used to refuse a connection or abort an existing one (no TIME-WAIT).

  • RTO (Retransmission Timeout): the time sender waits for an ACK of a segment before retransmitting. Accurate RTO avoids unnecessary retransmits and long delays.

  • SampleRTT, EstimatedRTT, DevRTT: SampleRTT is a measured round-trip time for a segment. EstimatedRTT is an exponentially weighted moving average of SampleRTT. DevRTT measures RTT variation (deviation).

  • MSL (Maximum Segment Lifetime): the maximum time a segment can exist in the network. It bounds the time to keep old state to avoid confusing delayed duplicates. Classic TCP (RFC 793) uses MSL = 2 minutes as an example; implementations vary (common 2·MSL values are 60–240 s).

Handshakes and state transitions (what to draw):

  • For the three-way handshake draw two boxes labeled "Client" (left) and "Server" (right). Draw three arrows from left to right and right to left: 1) Client -> Server: label "SYN, Seq = x"; 2) Server -> Client: label "SYN+ACK, Seq = y, Ack = x+1"; 3) Client -> Server: label "ACK, Seq = x+1, Ack = y+1". Under the arrows note that SYN consumes one sequence number and that after step 3 both sides are in the "ESTABLISHED" state. Also label states: Client: CLOSED -> SYN-SENT -> ESTABLISHED; Server: LISTEN -> SYN-RECEIVED -> ESTABLISHED.

  • For the four-way close draw two boxes and four arrows: 1) A -> B: "FIN, Seq = u"; 2) B -> A: "ACK, Ack = u+1"; 3) B -> A: "FIN, Seq = v"; 4) A -> B: "ACK, Ack = v+1". Show that after step 2 the first half is closed (A -> B no more data) and after step 4 connection is fully closed. Mark the side that enters TIME-WAIT (the endpoint that sends the final ACK). State names to annotate: A (active close): FIN-WAIT-1 -> FIN-WAIT-2 after step 2, then TIME-WAIT after step 4; B: CLOSE-WAIT after step 1, then LAST-ACK after step 3.

Exact statement about the three-way handshake (university-style):

  • To establish a reliable, full-duplex connection between two endpoints, TCP uses a three-step exchange: (1) a client sends SYN with initial sequence number x; (2) the server responds with SYN+ACK, choosing initial sequence number y and acknowledging x+1; (3) the client sends ACK acknowledging y+1. Assumptions: network may drop, delay or reorder segments; duplicate segments are possible. This handshake accomplishes two goals: (a) synchronizes initial sequence numbers of both sides, (b) confirms bidirectional connectivity (server saw client's SYN and client saw server's SYN+ACK).

TIME-WAIT (exact definition):

  • TIME-WAIT is a TCP state that lasts for 2·MSL after the endpoint that sends the final ACK (typically the active closer; in simultaneous close, both sides enter TIME-WAIT). Purpose: (1) allow retransmission of the final ACK (in case it was lost), (2) ensure delayed duplicate segments from the old connection are not accepted into a new connection with the same socket pair.

Simultaneous open and simultaneous close (brief):

  • Simultaneous open: both endpoints send SYNs at once. Each side receives SYN and responds with SYN+ACK; the remaining ACKs complete the handshake—outcome is still ESTABLISHED but initial seq numbers negotiated accordingly.
  • Simultaneous close: both endpoints send FIN nearly simultaneously; each ACKs the other's FIN, both may briefly enter CLOSING, then both move to TIME-WAIT.

Assumptions behind timers and correctness proofs:

  • RTO must be larger than typical RTT plus safety margin proportional to RTT variance to avoid spurious retransmits. Using EWMA for EstimatedRTT converges under bounded variance assumptions for SampleRTT and appropriate α in (0,1). In practice: do not measure SampleRTT for retransmitted segments (Karn/Partridge); on timeout, exponentially back off RTO (e.g., double) up to a cap; clamp RTO to a minimum (e.g., 1 s per RFC 6298).

Formulas

EstimatedRTT = (1 - α) * EstimatedRTT_prev + α * SampleRTT — EstimatedRTT in seconds; α dimensionless (typical α = 1/8). Use when updating RTT estimate after each measured SampleRTT.

DevRTT = (1 - β) * DevRTT_prev + β * |SampleRTT - EstimatedRTT| — DevRTT in seconds; β dimensionless (typical β = 1/4). Use to measure RTT variation.

TimeoutInterval = EstimatedRTT + 4 * DevRTT — RTO in seconds; applied when setting retransmission timer (Jacobson/Karels algorithm).

Time_WAIT_duration = 2 * MSL — seconds. MSL is maximum segment lifetime in the network.

Sequence_space = 2^32 — number of distinct sequence numbers in TCP (dimensionless); wrap-around occurs modulo 2^32.

Worked examples

  1. RTO calculation (numerical, step-by-step). Given: initial EstimatedRTT = 100 ms, initial DevRTT = 10 ms, α = 1/8, β = 1/4. Measurements: SampleRTT1 = 120 ms, SampleRTT2 = 90 ms. Compute new EstimatedRTT, DevRTT, TimeoutInterval after each sample. Steps (after SampleRTT1 = 120 ms):
  • EstimatedRTT = (1 - 1/8)*100 + (1/8)*120 = (7/8)*100 + 15 = 87.5 + 15 = 102.5 ms.
  • DevRTT = (1 - 1/4)10 + (1/4)|120 - 102.5| = 0.7510 + 0.2517.5 = 7.5 + 4.375 = 11.875 ms.
  • TimeoutInterval = 102.5 + 4*11.875 = 102.5 + 47.5 = 150.0 ms. Answer: TimeoutInterval = 150.0 ms. Steps (after SampleRTT2 = 90 ms):
  • EstimatedRTT = 0.875102.5 + 0.12590 = 89.6875 + 11.25 = 100.9375 ms.
  • DevRTT = 0.7511.875 + 0.25|90 - 100.9375| = 8.90625 + 0.25*10.9375 = 8.90625 + 2.734375 = 11.640625 ms.
  • TimeoutInterval = 100.9375 + 4*11.640625 = 100.9375 + 46.5625 = 147.5 ms. Answer: TimeoutInterval = 147.5 ms.
  1. Three-way handshake trace (theory, concrete numbers). Suppose Client chooses ISN x=1000, Server chooses ISN y=4000.
  • Step 1: Client -> Server: SYN, Seq = 1000. (SYN consumes one sequence number.)
  • Step 2: Server -> Client: SYN+ACK, Seq = 4000, Ack = 1001. (Server acknowledges client's SYN by Ack = x+1.)
  • Step 3: Client -> Server: ACK, Seq = 1001, Ack = 4001. (Client acknowledges server's SYN.) After step 3 both are ESTABLISHED. If any step is lost the sender retransmits according to RTO and sequence/ack fields remain valid.
  1. Connection release trace (four-way) with TIME-WAIT. Client A wants to close; A's next Seq = u.
  • A -> B: FIN, Seq = u. (Upon receipt, B enters CLOSE-WAIT.)
  • B -> A: ACK, Ack = u+1. (A transitions from FIN-WAIT-1 to FIN-WAIT-2.)
  • B -> A: FIN, Seq = v. (B transitions to LAST-ACK.)
  • A -> B: ACK, Ack = v+1. A enters TIME-WAIT for 2*MSL and retransmits this final ACK if needed. B enters CLOSED upon receiving this ACK. After TIME-WAIT expires the connection resources can be freed.

Common mistakes

  • Thinking FIN does not consume a sequence number — it does, just like SYN.
  • Forgetting which endpoint enters TIME-WAIT: the endpoint that sends the final ACK (typically the active closer; in simultaneous close, both sides do).
  • Using a fixed small RTO (e.g., 100 ms) without accounting for DevRTT — causes spurious retransmissions on variable networks.
  • Mixing up roles in simultaneous open/close traces (draw client and server columns to avoid swap mistakes).

For your exam

Typical question types: definitions (define SYN, FIN, TIME-WAIT), short notes (explain three-way handshake or TIME-WAIT), derivations (show RTO formula derivation from EWMA), numerical problems (compute RTO given samples), and diagram traces (draw handshake or close exchange, label sequence/ack numbers).

What full-marks answers include:

  • Clear definitions with bold terms (SYN, FIN, RTO, MSL). State assumptions (network may reorder/drop). For diagrams: draw two vertical columns labelled with endpoint names, arrows for segments with flags and Seq/Ack values. For numerical RTO: show initial values, α/β used, each arithmetic step and final units.

Quick check

  1. What three fields in TCP header are essential to establish a connection? (name flags/fields)
  2. How many segments are exchanged in the normal TCP connection establishment?
  3. Why is TIME-WAIT necessary? Give two reasons. Answers: 1) SYN flag, Seq (initial sequence number), Ack (acknowledgement number) — treat SYN and ACK flags with sequence/ack fields. 2) Three segments (SYN, SYN+ACK, ACK). 3) To allow retransmission of final ACK if lost, and to prevent delayed duplicate segments from being misinterpreted by a new connection (TIME-WAIT = 2*MSL).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?