TCP must agree on starting sequence numbers before sending data, close connections cleanly, and avoid overflowing the receiver.
The Three-Way Handshake
TCP opens a connection with three segments. 1. SYN: the client sends a segment with the SYN flag and its initial sequence number (ISN), say x. 2. SYN-ACK: the server replies with SYN and ACK set, its own ISN y, and acknowledgement number x + 1. 3. ACK: the client acknowledges with acknowledgement number y + 1. Now both sides know each other's starting sequence numbers, and data can flow. Random ISNs protect against old segments and some attacks. A SYN flood attack sends many SYNs without completing the handshake, filling the server's queue of half-open connections; SYN cookies defend against it.
Definition: Three-way handshake. The TCP connection setup in which the client sends SYN, the server replies with SYN-ACK, and the client sends ACK, so that both sides agree on initial sequence numbers.
Connection Termination
Since TCP is full-duplex, each direction is closed separately, normally with four segments: the side that finishes sends FIN; the other replies ACK; when it too has finished, it sends its own FIN; and the first side replies ACK. The side that closed first waits in the TIME_WAIT state (twice the maximum segment lifetime) so that any delayed segments die out. A connection can also be aborted at once with RST. The connection moves through states such as LISTEN, SYN_SENT, SYN_RECEIVED, ESTABLISHED, FIN_WAIT, CLOSE_WAIT, TIME_WAIT and CLOSED, which can be seen with the netstat command.
Reliability
TCP numbers bytes, not segments. The acknowledgement number is the next byte expected, so acknowledgements are cumulative. The sender starts a retransmission timer for each segment; its timeout (RTO) is calculated from measured round-trip times. Three duplicate ACKs signal a lost segment before the timer expires.
Flow Control
In flow control, the receiver tells the sender, in every segment, its receive window (rwnd): the free space in its buffer. The sender may have at most rwnd bytes unacknowledged. As the application reads data, the window opens; if the buffer fills, the receiver advertises a zero window and the sender stops, sending small window probes until space returns. This is a sliding window in bytes. The maximum throughput of one connection is about window / RTT.
Table 3.5: TCP flags
| Flag | Meaning |
|---|---|
| SYN | Synchronise sequence numbers, open a connection |
| ACK | Acknowledgement number is valid |
| FIN | Sender has finished sending |
| RST | Reset (abort) the connection |
| PSH | Push data to the application at once |
| URG | Urgent pointer is valid |
Example 3.5: Sequence and acknowledgement numbers
Problem: A client with ISN 1000 connects to a server with ISN 5000, then sends 300 bytes of data. Give the sequence and acknowledgement numbers of the handshake and the first data exchange.
Solution:
- SYN: seq = 1000.
- SYN-ACK: seq = 5000, ack = 1001.
- ACK: seq = 1001, ack = 5001.
- The data segment carries bytes 1001–1300: seq = 1001, length 300.
- The server acknowledges with ack = 1301, the next byte expected.
Result: The SYN consumes one sequence number; the ACK after the data is 1301.
cisn, sisn, data = 1000, 5000, 300
steps = [("client", "SYN", cisn, None),
("server", "SYN-ACK", sisn, cisn + 1),
("client", "ACK", cisn + 1, sisn + 1),
("client", f"DATA {data}B", cisn + 1, sisn + 1),
("server", "ACK", sisn + 1, cisn + 1 + data)]
for who, kind, seq, ack in steps:
a = ack if ack else "-"
print(f"{who:<6} {kind:<9} seq={seq:<5} ack={a}")
rtt, rwnd = 0.05, 65535
print(f"max throughput = {rwnd * 8 / rtt / 1e6:.1f} Mbps "
f"(window {rwnd} B, RTT {rtt * 1000:.0f} ms)")
Output
client SYN seq=1000 ack=-
server SYN-ACK seq=5000 ack=1001
client ACK seq=1001 ack=5001
client DATA 300B seq=1001 ack=5001
server ACK seq=5001 ack=1301
max throughput = 10.5 Mbps (window 65535 B, RTT 50 ms)
Common mistakes
- Thinking the acknowledgement number is the last byte received. It is the next byte expected.
- Forgetting that SYN and FIN each consume one sequence number.
- Confusing flow control (protecting the receiver) with congestion control (protecting the network).
Key takeaways
- Handshake: SYN, SYN-ACK, ACK; termination: FIN, ACK, FIN, ACK.
- TCP numbers bytes; ACKs are cumulative and give the next byte expected.
- Flow control uses the receiver's advertised window (rwnd).
- Throughput is limited to about window / RTT.
Practice questions
- What are the three segments of the TCP handshake?
- What is a SYN flood?
- What does a zero window mean?
Answers to the practice questions
- SYN, SYN-ACK and ACK.
- An attack that sends many SYNs without completing handshakes, exhausting the server's half-open connection queue.
- The receiver's buffer is full, so the sender must stop sending.
Sources
- Official BCA syllabus Computer Networks, course code 26BCA404DS02, Unit III
