Flow Control & Connection Management
These notes are low-effort, due to catching up in this module. See the videos and slides for more detail.
TCP Flow Control
This is where the receiver sends messages to the sender to slow the rate of transfer. This is so that the incoming data doesn’t overflow the buffer by sending data quicker than it can be processed.
- Free space is advertised in the
rwnd
field of the TCP header. RcvBuffer
is set by the socket options (typically 4096 bytes).- Many operating systems auto adjust
RcvBuffer
.
flowchart subgraph RcvBuffer us[Used Space] rwnd["rwnd (Free Space)"] end p[TCP Segment Payloads] --> RcvBuffer RcvBuffer --> a[Application Process]
- Many operating systems auto adjust
-
Sender limits the amount of unACKed data to received
rwnd
.This guarantees the receive buffer will not overflow.
TCP Connection Management
Before exchanging data the sender and receiver need to handshake. This includes:
- Accepting the incoming connection.
- Agreeing on connection parameters.
Two-Way Handshake
A 2-way handshake is problematic as is gives issues when used over an unreliable medium. Ordinarily it would work like so over a port number $x$:
sequenceDiagram
Client ->> Server: req_conn(x)
note right of Server: ESTAB
Server ->> Client: acc_conn(x)
note left of Client:ESTAB
This can produce half-open connections and duplicate data if the handshake messages are lost and re-transmitted.
Three-Way Handshake
This is a more redundant handshake method than resolved the issue of a two way handshake:
sequenceDiagram
note left of Client: Choose init seq num x, send TCP SYN msg
Client ->> Server: SYNbit=1, Seq=x
note right of Client: SYN SENT
note right of Server: Choose init seq num y, send TCP SYNACK msg ACKing SYN
note left of Server: SYN RCVD
Server ->> Client: SYNbit=1, Seq=y, ACKbit=1, ACKnum=x+1
note right of Client: ESTAB
note left of Client: Received SYNACK(x) indicates server is live.<br>Send ACK for SYNACK, this segment may contain client-to-server data.
Client ->> Server: ACKbit=1, ACKnum=y+1
note left of Server: ESTAB
note right of Server: Received ACK(y) indicates client is live.
Closing a TCP Connection
- Client and server each close their side of the connection:
- Send TCP segment with
FIN
bit = 1
- Send TCP segment with
- Respond to received
FIN
withACK
.- On receiving
FIN
,ACK
can be combined with onFIN
.
- On receiving
Simultaneous FIN
exchanges can be handled.
sequenceDiagram
Client ->> Server: FINbit=1, seq=x
note left of Client: Can no longer send but can receive data.
note right of Server: Can still send data.
note right of Client: FIN_WAIT_1
note left of Server: CLOSE_WAIT
Server ->> Client: ACKbit=1, ACKnum=x+1
note left of Client: Wait for server to close.
note right of Client: FIN_WAIT_2
note left of Server: LAST_ACK
Server ->> Client: FINbit=1, seq=x
note right of Server: Can no longer send data.
note left of Client: Timed wait for 2*max segment lifetime.
note right of Client: TIMED_WAIT
Client ->> Server: ACKbit=1, ACKnum=y+1
note left of Server: CLOSED
note right of Client: CLOSED