3 · TCP flow control and connection management
Slides 3-57 → 3-64 · K&R 3.5.5, 3.5.6 · HW4 P43 · Sample midterm 1.3
The problem flow control solves (3-57)
- Question: what if the network layer delivers data faster than the application reads it out of the socket buffer? The receive buffer overflows and data is lost.
- Flow control: the receiver controls the sender, so the sender won't overflow the receiver's buffer by transmitting too much, too fast.
- It is a speed-matching service: it matches the sender's rate to the rate at which the receiving app reads.
How TCP does it: rwnd (3-58, 3-59)
- The receiver advertises its free buffer space in the receive window field (rwnd) of every TCP header it sends back.
- RcvBuffer is set via socket options. The typical default is 4096 bytes, and many OSes autoadjust it.
- Receiver side: LastByteRead = last byte the app pulled out; LastByteRcvd = last byte that arrived and was placed in the buffer. TCP always keeps
LastByteRcvd − LastByteRead ≤ RcvBuffer. - The formula:
rwnd = RcvBuffer − [LastByteRcvd − LastByteRead]. The bracket is the data sitting in the buffer, not yet read. - Sender side: it keeps unACKed ("in-flight") data at or below rwnd:
LastByteSent − LastByteAcked ≤ rwnd. This guarantees the receive buffer will not overflow. - rwnd is dynamic: it shrinks as data arrives and grows as the app reads.
The rwnd = 0 corner case (K&R 3.5.5)
- If B advertises rwnd = 0 and has nothing else to send, B never sends another segment, so A would never learn that space opened up. Deadlock.
- Fix: while rwnd = 0, A keeps sending segments with 1 data byte. B ACKs them, and once the buffer drains an ACK carries a nonzero rwnd.
- UDP has no flow control: if the app reads too slowly, the socket buffer overflows and segments are dropped.
Connection management: why handshake at all (3-60)
- Before exchanging data, both sides agree to establish the connection (each knows the other is willing) and agree on parameters, chiefly the starting sequence numbers.
- Connection state each side keeps: ESTAB, seq # client-to-server and server-to-client, rcvBuffer size at server and client.
- Client opens with
connect(); server accepts withaccept()on its welcoming socket.
Why a 2-way handshake fails (3-61, 3-62)
- 2-way = client sends
req_conn(x), server repliesacc_conn(x), both go ESTAB. - The network has variable delays, retransmitted messages (from loss), reordering, and neither side can "see" the other.
- Failure 1, half-open connection: client's
req_conn(x)is slow, so the client retransmits it. The connection completes and closes; the client terminates and the server forgets x. Then the old retransmitted req_conn(x) arrives, the server goes ESTAB with no client. - Failure 2, old data accepted: same as above, but a retransmitted
data(x+1)also arrives late. The server opens a new connection from the stalereq_conn(x)and accepts the duplicate data(x+1) as new. - Root cause: the server goes ESTAB without proof that the client is live now. The third message supplies that proof.
TCP 3-way handshake (3-63)
| # | Segment | Fields | What it means |
|---|---|---|---|
| 1 | Client → server: SYN | SYNbit=1, Seq=x | Client picks an initial seq # x. No app data. Client goes SYN_SENT. |
| 2 | Server → client: SYNACK | SYNbit=1, Seq=y, ACKbit=1, ACKnum=x+1 | Server picks its own ISN y, ACKs the SYN, allocates buffers. Server goes SYN_RCVD. |
| 3 | Client → server: ACK | ACKbit=1, ACKnum=y+1 (Seq=x+1) | "Server is live." Client goes ESTAB. May carry client-to-server data. On receipt the server knows the client is live and goes ESTAB. |
- The SYN consumes one sequence number even though it carries no data. That is why the ACK is x+1, not x.
- ISNs are chosen (randomly), not 0, so stale segments from an old connection are unlikely to fall inside the new one's seq space.
- Socket calls: server does
socket, bind, listen, accept(LISTEN); client doessocket, connect(which sends the SYN).
Closing a connection (3-64)
- Each side closes its own direction by sending a segment with FIN bit = 1.
- The other side responds to a FIN with an ACK. On receiving a FIN, the ACK can be combined with its own FIN.
- Simultaneous FIN exchanges are handled.
- Typical sequence: client FIN → server ACK → server FIN → client ACK → client waits in TIME_WAIT → CLOSED.
- TIME_WAIT (timed wait): the client lingers (typically 30 s, 1 min, or 2 min; about 2 × max segment lifetime) so it can resend the final ACK if it was lost and let stray old segments die out.
- Disconnect bit = FIN (sample midterm 1.3).
State machines (K&R Fig. 3.41, 3.42)
Client: CLOSED --send SYN--> SYN_SENT --rcv SYNACK, send ACK--> ESTABLISHED
ESTABLISHED --send FIN--> FIN_WAIT_1 --rcv ACK--> FIN_WAIT_2
FIN_WAIT_2 --rcv FIN, send ACK--> TIME_WAIT --wait--> CLOSED
Server: CLOSED --listen()--> LISTEN --rcv SYN, send SYNACK--> SYN_RCVD
SYN_RCVD --rcv ACK--> ESTABLISHED
ESTABLISHED --rcv FIN, send ACK--> CLOSE_WAIT --send FIN--> LAST_ACK
LAST_ACK --rcv ACK--> CLOSED
- The side that sends FIN first ends up in TIME_WAIT. The side that receives it goes through CLOSE_WAIT and LAST_ACK.
- Slide 3-63 labels the client's initial state LISTEN; in the book it is CLOSED. Only the server LISTENs.
The six flag bits, and RST
- Flags in the TCP header: URG, ACK, PSH, RST, SYN, FIN (plus CWR/ECE for ECN).
- SYN = set up, FIN = graceful close, ACK = ACKnum field is valid.
- RST = reset. A host sends it when a SYN (or any segment) arrives for a port with no matching socket: "I have no socket for that, don't resend." It is an abrupt abort, not a graceful close.
- SYN flood: attacker sends many SYNs and never the third ACK, tying up half-open state. Defense: SYN cookies.
How to solve: compute rwnd and how much the sender may still send
- Buffered (unread) bytes =
LastByteRcvd − LastByteRead. rwnd = RcvBuffer − buffered.- At the sender: in-flight =
LastByteSent − LastByteAcked. - Bytes the sender may still send now =
rwnd − in-flight(0 if negative).
Example (default RcvBuffer from 3-58): RcvBuffer = 4096. LastByteRcvd = 3000, LastByteRead = 1000. Sender has LastByteAcked = 3000, LastByteSent = 3800.
buffered = 3000 − 1000 = 2000 bytes rwnd = 4096 − 2000 = 2096 bytes in-flight = 3800 − 3000 = 800 bytes may send = 2096 − 800 = 1296 more bytes
How to solve: fill in a handshake
- SYN:
Seq = client ISN, no ACKnum. - SYNACK:
Seq = server ISN,ACKnum = client ISN + 1. - ACK:
Seq = client ISN + 1,ACKnum = server ISN + 1. - First data byte from the client has seq
client ISN + 1; from the server,server ISN + 1.
Example: client ISN x = 100, server ISN y = 500.
SYN : SYNbit=1, Seq=100 SYNACK : SYNbit=1, Seq=500, ACKbit=1, ACKnum=101 ACK : ACKbit=1, Seq=101, ACKnum=501
HW4 P43: what throttles the sending process?
Q (HW4 P43): Host A is sending an enormous file to Host B over a TCP connection. On this connection there is never any packet loss and the timers never expire. The link connecting Host A to the Internet transmits at R bps. The process in Host A can send data into its TCP socket at S bps, where S = 10R. The TCP receive buffer is large enough to hold the entire file, and the send buffer can hold only 1% of the file. What would prevent the process in Host A from continuously passing data to its TCP socket at rate S bps? TCP flow control? TCP congestion control? Or something else? Elaborate.
- Not flow control: B's receive buffer can hold the entire file, so rwnd never limits A.
- Not congestion control: no loss and ACKs return before timers expire, so cwnd never gets cut.
- Something else: the send buffer. Data enters it at S = 10R but leaves onto the link at only R, so it fills quickly. Once full, the process's writes block, and it passes data at an average rate of R << S.
• rwnd is the receiver's free space, not its total buffer. Subtract the unread bytes.
• Subtract LastByteRead, not LastByteAcked. The ACKed-but-unread bytes still occupy the buffer.
• SYNACK's ACKnum is x+1, the ACK's ACKnum is y+1. The SYN eats a seq number though it has no data.
• The third handshake segment can carry data; the SYN cannot.
• Disconnect = FIN, not RST. RST is an abort / "no such socket".
• Flow control is about the receiver; congestion is about the network. In P43, it's neither: the local send buffer.
• With rwnd = 0 the sender doesn't go silent; it sends 1-byte probes.
• TIME_WAIT is on the side that sent the first FIN.
Quick check
1. In the TCP header, which bit is set to 1 to represent a disconnect? (sample 1.3)
The FIN bit. Each side sends a FIN to close its direction; the other side ACKs it.2. What prevents a TCP sender from overflowing the receiver's buffer?
Flow control: the receiver advertises free space in the rwnd (receive window) field, and the sender keepsLastByteSent − LastByteAcked ≤ rwnd.3. RcvBuffer = 8192 bytes, LastByteRcvd = 6000, LastByteRead = 2500. What rwnd does the receiver advertise?
buffered = 6000 − 2500 = 3500 rwnd = 8192 − 3500 = 4692 bytes
4. Using the rwnd from Q3, the sender has LastByteAcked = 6000 and LastByteSent = 8000. How many more bytes may it send right now?
in-flight = 8000 − 6000 = 2000 may send = 4692 − 2000 = 2692 bytes
5. Client ISN = 3000, server ISN = 7200. Fill in Seq and ACKnum for all three handshake segments. If the third segment carries 100 bytes of data, what is the client's next seq #?
SYN : SYNbit=1, Seq=3000 SYNACK : SYNbit=1, Seq=7200, ACKbit=1, ACKnum=3001 ACK : ACKbit=1, Seq=3001, ACKnum=7201 (+100 data bytes)Next client seq = 3001 + 100 = 3101.
6. A RcvBuffer of 4096 is completely full and the app is busy. What does B advertise, and what does A do?
B advertises rwnd = 0. A keeps sending 1-byte segments; B ACKs them, and once the app reads, an ACK reports a nonzero rwnd and A resumes.7. Give one way a 2-way handshake fails.
A delayed, retransmitted req_conn(x) arrives after the client's connection has finished and the client is gone. The server goes ESTAB with no client: a half-open connection. (Or a stale data(x+1) is then accepted as new data.)8. Flow control vs congestion control, one line.
Flow control: one sender too fast for one receiver (rwnd). Congestion control: too many senders too fast for the network (cwnd).9. HW4 P43 variant: S = 5R, receive buffer holds the whole file, no loss. What limits the process's write rate, and to what?
The send buffer fills (in at 5R, out at R), so the process's writes block and it averages R. Not flow control (huge receive buffer), not congestion control (no loss).10. Which side ends up in TIME_WAIT, and why wait?
The side that sent the first FIN (usually the client). It waits so it can resend the final ACK if that ACK was lost, and so old segments die out before the port pair is reused.11. What does an RST segment mean?
Reset: the host has no socket matching the segment (e.g. a SYN to a closed port), or is aborting the connection. Not a graceful close.12. Which handshake segment may carry application data?
The third one (client's ACK). The SYN and SYNACK carry none.← prev · all topics · next →