Selective Repeat

SR is like GBN, but “smarter” with out‑of‑order packets. The sender retransmits only missing packets, and the receiver can buffer packets that arrive early.

Sender

Think of the sender like this:

  1. Split the file into packets.
  2. Send packets while there is room in the window.
  3. Track which packets are ACKed individually.
  4. If a particular packet times out, resend just that one.
  5. As ACKs arrive, slide the window forward past any already‑ACKed packets.
    • Receiver sends ACK for the correctly received packet’s sequence number.
  6. Finish with FIN/FINACK once all data is confirmed.

The key idea: “per‑packet ACKs + per‑packet retransmit.”

Receiver

On the receiver side:

  1. If the packet is within your receive window, store it.
  2. ACK the correctly received packet immediately (even if it’s a duplicate).
  3. When the next expected packet is present, write it (and any consecutive buffered packets).
  4. Packets outside the window can be dropped or ignored.
  5. Handle FIN with FINACK once everything is delivered

Fast Restransmit

Some SR variants add a “fast retransmit” rule: if the sender sees repeated ACKs that point to the same missing packet, it can resend that packet early, without waiting for its timeout. This is optional but can improve performance when there’s moderate loss.

At a high level:

  • If you observe several ACKs that all indicate the same gap, treat it as a signal that one packet is missing.
  • Retransmit the missing packet immediately.
  • Keep your normal timeout‑based retransmissions as a backup.