TCP: segments, handshake and teardown
What TCP provides (a reliable, ordered byte stream between two processes over unreliable IP), the segment header fields and flags, sequence numbers that count bytes and acknowledgement numbers that name the next byte expected, the maximum segment size, the three-way handshake (SYN and FIN each use one sequence number), data transfer, the four-segment connection close and the main states of the TCP state machine, and the wrap-around time of the 32-bit sequence numbers; the segment exchange is generated by a model of the protocol with 32-bit arithmetic and checked on random transfers.
Why it exists
IP delivers datagrams between hosts without promises: they may be lost, duplicated or reordered. Applications want to send a stream of bytes and get the same stream out at the other end, in order and without gaps, between a particular process on one host and a process on another. TCP (transmission control protocol) builds that service on top of IP. It adds connections, ports, reliability (acknowledgements and retransmission, as in sliding window protocols), flow control (TCP flow control) and congestion control (TCP congestion control).
Prerequisites
You need IP datagrams, the MTU and their headers (IPv4 header and fragmentation).
Precise definitions
TCP is connection-oriented (a connection is set up before data are sent), provides a full-duplex byte stream (data flow in both directions at once) and is point-to-point (exactly two endpoints). An endpoint is identified by an IP address and a port number (16 bits); a connection by the pair of endpoints (source IP, source port, destination IP, destination port).
The TCP segment. The data from the application are cut into segments; each has a header (20 bytes without options, up to 60 with) and is carried in an IP datagram. Header fields (quoted):
| field | size | meaning |
|---|---|---|
| source port, destination port | 16 bits each | identify the processes |
| sequence number | 32 bits | number of the first data byte of the segment in the stream |
| acknowledgement number | 32 bits | number of the next byte expected from the other side (valid if ACK is set) |
| header length | 4 bits | in 4-byte words |
| flags | 6 bits | URG, ACK, PSH, RST, SYN, FIN |
| window | 16 bits | receive window (bytes the receiver is willing to accept: flow control) |
| checksum | 16 bits | Internet checksum over header, data and a pseudo-header with the IP addresses |
| urgent pointer, options | for example the maximum segment size, window scale, timestamps |
Sequence and acknowledgement numbers count bytes, not segments. If a segment carries bytes its sequence number is and the acknowledgement that confirms it has the number . The acknowledgement is cumulative: number means "all bytes up to arrived". The numbers are 32 bits and wrap around modulo . The connection starts at an initial sequence number (ISN) chosen by each side (not , to avoid confusion with old connections).
The maximum segment size (MSS) is the largest number of data bytes in a segment. It is chosen so that the segment and the two headers fit in the MTU: ( for Ethernet), so that IP does not need to fragment.
How it works
The connection
A TCP connection: handshake, 1200 bytes up, 300 bytes down, close: start
Computed by a script, not typed| segment | direction | flags | sequence number | acknowledgement number | data bytes |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
| 6 | |||||
| 7 | |||||
| 8 | |||||
| 9 | |||||
| 10 | |||||
| 11 | |||||
| 12 | |||||
| 13 | |||||
| 14 | |||||
| 15 |
Step 0
Client initial sequence number 2000, server 8000, maximum segment size 500 bytes. Sequence numbers count BYTES; the acknowledgement number is the next byte expected.
Three-way handshake (open). (1) The client sends a segment with SYN and its initial number (). (2) The server replies with SYN+ACK: its own initial number () and acknowledgement , because a SYN consumes one sequence number, so the next byte the server expects is . (3) The client sends an ACK with acknowledgement (the server's SYN also used a number); this segment may already carry data. Three segments are needed so that both sides know that the other has received and chosen its sequence numbers.
Data transfer. The client sends bytes as segments of bytes with sequence numbers ; the acknowledgements show the cumulative next-byte-expected rule. The server's reply of bytes starts with sequence number and is acknowledged with . (Real TCP often acknowledges every second segment or delays the acknowledgement; this model acknowledges every segment.)
Closing. Each direction is closed separately, so closing takes four segments: the client's FIN (it consumes one sequence number: the client's next number is ), the server's ACK , the server's FIN (number ) and the client's final ACK . After the last ACK the client waits in the TIME_WAIT state for twice the maximum segment lifetime (quoted) in case the last ACK was lost. A side may also abort with a segment having the RST flag.
States (quoted from the textbooks)
| state | meaning |
|---|---|
| CLOSED | no connection |
| LISTEN | server waits for a SYN |
| SYN_SENT | client has sent SYN |
| SYN_RCVD | server received SYN, sent SYN+ACK |
| ESTABLISHED | data transfer |
| FIN_WAIT_1 / FIN_WAIT_2 | the side that closed first: FIN sent / its FIN acknowledged |
| CLOSE_WAIT | received FIN, application has not closed yet |
| LAST_ACK | the second side sent its FIN, waits for the ACK |
| TIME_WAIT | the side that closed first waits MSL before CLOSED |
| CLOSING | both sides closed at the same time |
Segment size
Maximum segment size from the MTU: start
Computed by a script, not typed| link | MSS (bytes) | segments for 100 000 bytes | data share of an Ethernet frame |
|---|---|---|---|
| MTU 1500 | |||
| MTU 1280 | |||
| MTU 576 |
Step 0
MSS = MTU - 20 (IP header) - 20 (TCP header, no options). Both headers at their minimum size.
Wrap-around
Sequence numbers are 32 bits, so a sender that keeps a link full uses all numbers in seconds:
Time for the 32-bit sequence numbers to wrap around: start
Computed by a script, not typed| link rate | wrap-around time | shorter than the 120 s segment lifetime |
|---|---|---|
| 10 Mbps | ||
| 100 Mbps | ||
| 1 Gbps | ||
| 10 Gbps |
Step 0
Time = 2^32 bytes × 8 bits / rate. This is the reason for the timestamp option on fast links (quoted).
On a Gbps link they wrap in s, less than the (conventional) maximum segment lifetime of s. A very old segment could then carry a sequence number that is valid again. At Mbps this takes almost an hour. This is why fast links need extra protection (the timestamp option).
Worked examples
EX 1Acknowledgement number
EasyA segment carries bytes with sequence number : the bytes . The acknowledgement that confirms it has number . If the next segment () is lost and the one after it () arrives, the receiver still acknowledges (duplicate acknowledgement).
EX 2Handshake numbers
MediumClient ISN , server ISN : SYN has seq ; SYN+ACK has seq , ack ; the third segment has seq , ack ; the first data byte of the client has number and of the server . After bytes from the client and a FIN the client's next number is .
EX 3Segments and MSS
Mediumbytes with MSS : segments ( full and one of bytes). The data share of an Ethernet frame with a -byte segment is percent.
EX 4Typical questions
GATE level(a) "A client sends SYN with sequence number . What are the sequence and acknowledgement numbers of the SYN+ACK if the server's ISN is ?" seq , ack . (b) "How many segments are exchanged to open and to close a connection?" and . (c) "Does TCP count segments or bytes in its sequence numbers?" Bytes. (d) "How long until the sequence numbers wrap at Gbps?" s. (e) "What does the receiver acknowledge when segment is lost but arrives?" The same number as for segment (the first missing byte). (f) "Which flag is set in the second segment of the handshake?" SYN and ACK.
Cheat box
Cheat box
- Reliable, ordered byte stream, full duplex, connection-oriented; 20-byte header; ports 16 bits.
- Sequence number = first byte of the segment; acknowledgement = next byte expected (cumulative).
- SYN and FIN each consume one sequence number; handshake: SYN, SYN+ACK, ACK; close: FIN, ACK, FIN, ACK.
- MSS MTU . Wrap-around of bits at rate : s.
- ACK-only segments carry no data and use no sequence number.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
No cross-subject links yet.
TCP's reliability is the sliding window idea with cumulative acknowledgements (sliding window protocols). The window field limits the sender (flow control) and the congestion window adapts to the network (congestion control). Applications use TCP through sockets (socket API); UDP offers ports without these services.
PYQs
17 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 12 by reasoning only); the book's key is shown next to my answer. All PYQs
With respect to a TCP connection between a client and a server, which one of the following statements is true?
- (a)The client and server use a two-way handshake mechanism before the start of data transmission
- (b)The server cannot initiate closing of the connection before the client initiates closing of the connection
- (c)The TCP connection is half-duplex
- (d)The client and server can initiate closing of the connection at the same time
It is necessary to design a link-layer protocol between two hosts that are directly connected over a lossless link of length 3000 km. The link bandwidth is 108 bits per second, and the propagation delay is 5 ns per meter. Every transmitted data byte is assigned a unique sequence number. Let N be the minimum number of bits needed for the sequence number field such that: i. the sequence numbers do not wrap around before 60 seconds, and ii. the maximum utilization of the link is achieved. Find N. (answer in integer)
Consider the following statements: (i) Address Resolution Protocol (ARP) provides a mapping from an IP address to the corresponding hardware (link-layer) address. (ii) A single TCP segment from a sender S to a receiver R cannot carry both data from S to R and acknowledgment for a segment from R to S. Which ONE of the following is CORRECT?
- (a)Both (i) and (ii) are TRUE
- (b)(i) is TRUE and (ii) is FALSE
- (c)(i) is FALSE and (ii) is TRUE
- (d)Both (i) and (ii) are FALSE
Consider the 3-way handshaking protocol for TCP connection establishment. Let the three packets exchanged during the connection establishment be denoted as P1, P2, and P3, in order. Which of the following option(s) is/are TRUE with respect to TCP header flags that are set in the packets?
- (a)P3: SYN = 1, ACK = 1
- (b)P2: SYN = 1, ACK = 1
- (c)P2: SYN = 0, ACK = 1
- (d)P1: SYN = 1
TCP client P successfully establishes a connection to TCP server Q. Let NP denote the sequence number in the SYN sent from P to Q. Let NQ denote the acknowledgment number in the SYN ACK from Q to P. Which of the following statements is/are CORRECT?
- (a)The acknowledgment number NQ is equal to NP
- (b)The sequence number NP is always 0 for a new connection
- (c)The acknowledgment number NQ is equal to NP + 1
- (d)The sequence number NP is chosen randomly by P
Suppose You are asked to design a new reliable byte-stream transport protocol like TCP, named myTCP, which runs over a 100 Mbps network with a Round Trip Time (RTT) of 150 milliseconds and a maximum segment lifetime of 2 minutes. Which of the following is/are valid lengths of the Sequence Number field in the myTCP header?
- (a)30 bits
- (b)32 bits
- (c)34 bits
- (d)36 bits
Consider the data transfer using TCP over a 1 Gbps link. Assuming that the maximum segment lifetime (MSL) is set to 60 seconds, the minimum number of bits required for the sequence number field of the TCP header, to prevent the sequence number space from wrapping around during the MSL is ___________.
A TCP server application is programmed to listen on port number P on host S. A TCP client is connected to the TCP server over the network. Consider that while the TCP connection was active, the server machine S crashed and rebooted. Assume that the client does not use the TCP keepalive timer. Which of the following behaviors is/are possible?
- (a)If the client sends a packet after the server reboot, it will receive a FIN segment.
- (b)If the client was waiting to receive a packet, it may wait indefinitely.
- (c)The TCP server application on S can listen on P after reboot.
- (d)If the client sends a packet after the server reboot, it will receive a RST segment.
Consider the three-way handshake mechanism followed during TCP connection establishment between hosts P and Q. Let X and Y be two random 32-bit starting sequence numbers chosen by P and Q respectively. Suppose P sends a TCP connection request message to Q with a TCP segment having SYN bit = 1, SEQ number = X, and ACK bit = 0. Suppose Q accepts the connection request. Which one of the following choices represents the information present in the TCP segment header that is sent by Q to P?
- (a)SYN bit = 1, SEQ number = Y, ACK bit = 1, ACK number = X + 1, FIN bit = 0
- (b)SYN bit = 1, SEQ number = Y, ACK bit = 1, ACK number = X, FIN bit = 0
- (c)SYN bit = 0, SEQ number = X + 1, ACK bit = 0, ACK number = Y, FIN bit = 1
- (d)SYN bit = 1, SEQ number = X + 1, ACK bit = 1, ACK number = Y, FIN bit = 0
Consider a long-lived TCP session with an end-to-end bandwidth of 1 Gbps = (109 bits per second). The session starts with a sequence number of 1234. The minimum time (in seconds, rounded to the closest integer) before this sequence number can be used again is
Match the following: Field Length (bits) P. UDP Header's Port Number i. 48 Q. Ethernet MAC Address ii. 8 R. IPv6 Next Header iii. 32 S. TCP Header's Sequence Number iv. 16 COMPUTER NETWORK
- (a)P-III, Q-IV, R-II, S-I
- (b)P-II, Q-I, R-IV, S-III
- (c)P-IV, Q-I, R-II, S-III
- (d)P-IV, Q-I, R-III, S-II
Consider a TCP client and a TCP server running on two different machines. After completing data transfer, the TCP client calls close to terminate the connection and a FIN segment is sent to the TCP server. Server-side TCP responds by sending an ACK, which is received by the client-side TCP. As per the TCP connection state diagram (RFC 793), in which state does the client-side TCP connection wait for the FIN from the server-side TCP?
- (a)LAST-ACK
- (b)TIME-WAIT
- (c)FIN-WAIT-1
- (d)FIN-WAIT-2
Consider the following statements: I. TCP connections are full duplex. II. TCP has no option for selective acknowledgment. III. TCP connections are message streams.
- (a)Only I is correct
- (b)Only I and II are correct
- (c)Only II and III are correct
- (d)All of I, II, and III are correct
Suppose two hosts use a TCP connection to transfer a large file. Which of the following statements is/are FALSE with respect to the TCP connection? 1. If the sequence number of a segment is m, then the sequence number of the subsequent segment is always m+1. 2. If the estimated round trip time at any given point of time is t sec, the value of the retransmission timeout is always set to greater than or equal to t sec. 3. The size of the advertised window never changes during the course of the TCP connection. 4. The number of unacknowledged bytes at the sender is always less than or equal to the advertised window.
- (a)3 only
- (b)1 and 3 only
- (c)1 and 4 only
- (d)2 and 4 only
While opening a TCP connection, the initial sequence number is to be derived using a time-of- day (ToD) clock that keeps running even when the host is down. The low order 32 bits of the counter of the ToD clock is to be used for the initial sequence numbers. The clock counter increments once per millisecond. The maximum packet lifetime is given to be 64 s. Which one of the choices given below is closest to the minimum permissible rate at which sequence numbers used for packets of a connection can increase?
- (a)0.015/s
- (b)0.064/s
- (c)0.135/s
- (d)0.327/s
Which of the following system calls results in the sending of SYN packets?
- (a)socket
- (b)bind
- (c)listen
- (d)connect
What is the maximum size of data that the application layer can pass on to the TCP layer below?
- (a)Any size
- (b)216 bytes - size of TCP header
- (c)216 bytes
- (d)1500 bytes
Self-check
Question 1
The client's ISN is ; it sends bytes and then closes. What is the sequence number of its FIN?
Show answerHide
SYN uses , data are , so FIN has sequence number .
Question 2
MTU . What MSS, and why not ?
Show answerHide
: the -byte IP header and -byte TCP header must fit in the MTU, so that IP does not fragment.
Question 3
Why does the close need four segments although the open needs three?
Show answerHide
Each direction is closed independently (a side may still have data to send after the other side has finished); the server's ACK and FIN can be sent separately.
Revision card
One-screen revision
- Byte stream; seq first byte, ack next byte expected; SYN and FIN use one number.
- Open: SYN, SYN+ACK, ACK. Close: FIN, ACK, FIN, ACK; TIME_WAIT.
- MSS MTU ; -bit numbers wrap in s.
References
- ·Kurose, Ross, Computer Networking: A Top-Down Approach (8th edition), Chapter 3, Section 3.5 (Connection-oriented transport: TCP)
- ·Tanenbaum, Wetherall, Computer Networks (5th edition), Chapter 6, Section 6.5 (The Internet transport protocols: TCP)
- ·Forouzan, Data Communications and Networking (5th edition), Chapter 24 (Transport layer: TCP)
Verification
scripts/enum-checks/cn_tcp_check.py: 2000 random transfers (initial sequence numbers including values within 3000 of 2^32 so that the numbers wrap around, 0 to 5000 bytes each way, MSS 100, 536 or 1460) were generated by scripts/cn_tcp.py (tcp_session) and replayed by a separate loop: every segment carries the sender's next sequence number, every acknowledgement number equals the next sequence number the peer is going to use (the next byte expected), data per segment never exceeds the MSS, the byte totals match, and SYN and FIN each consume exactly one sequence number (final numbers = initial + 1 + bytes + 1 on both sides). The wrap-around times 2^32 x 8 / rate (34.36 s at 1 Gbps, 3435.97 s at 10 Mbps) were recomputed. MODEL: no loss, no reordering, every data segment acknowledged separately (real TCP uses delayed and cumulative acknowledgements), the closing sequence FIN-ACK-FIN-ACK with the client closing first. QUOTED from the RFCs and textbooks, not verified here: the header layout and flag list, the options (MSS, window scale, timestamps, SACK), the state machine and its state names, the 2 MSL waiting time in TIME_WAIT, the maximum segment lifetime of 120 s used for the wrap-around comparison, the simultaneous open and close variations and RST segments. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (CRC remainders, binomial tail, checksums); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)