If one small part of the file disappears on the way, you do not want the final PDF to contain missing pages or corrupted data.
This is where TCP becomes useful. TCP keeps track of bytes, acknowledges received data, retransmits missing data and manages a logical connection between the two applications.
TCP or Transmission Control Protocol is a connection-oriented Transport Layer protocol that provides reliable and ordered byte-stream communication between application processes.
A TCP segment contains a TCP header followed by data. Important header fields include source port, destination port, sequence number, acknowledgement number, data offset, control flags, window size, checksum, urgent pointer and optional fields.
TCP normally establishes a connection using a three-way handshake and closes the two communication directions using FIN and ACK exchanges.
What Is TCP?
TCP stands for:
TCP is one of the main Transport Layer protocols used in TCP/IP networks.
Its job is not simply to send data.
TCP tries to make sure that data reaches the receiving application:
- Reliably
- In the correct order
- Without accidental duplication
- At a rate the receiver can handle
Where Does TCP Work?
Main Features of TCP
How TCP Works
TCP communication can be understood in three major stages.
What Is a TCP Segment?
The Transport Layer protocol data unit of TCP is normally called a:
A TCP segment contains:
Minimum 20 Bytes
Variable Length
TCP Segment Format
The TCP header is arranged in 32-bit rows.
The minimum TCP header is:
With options, the header can grow up to:
Source Port and Destination Port
Both port fields are 16 bits long.
The source port identifies the sending process, while the destination port identifies the receiving service or process.
Example
A browser may create a connection such as:
Server: 198.51.100.20 : 443
The client uses a temporary source port, while the HTTPS server commonly listens on port 443.
TCP Sequence Number
TCP does not number complete messages.
TCP numbers the bytes in its byte stream.
The Sequence Number field is 32 bits long.
For a segment carrying data, the sequence number normally identifies the number of the first data byte contained in that segment.
SYN and FIN Also Consume Sequence Space
A SYN flag used during connection establishment consumes one sequence number.
A FIN used to close a sending direction also consumes one sequence number.
TCP Acknowledgement Number
The Acknowledgement Number field is also 32 bits.
It normally means:
Example
Suppose the receiver has correctly received bytes 1000 through 1499.
1000 - 1199
1200 - 1499
1500
TCP acknowledgement is normally cumulative.
An ACK of 1500 tells the sender that the receiver expects byte 1500 next.
TCP Data Offset Field
Data Offset tells the receiver where the TCP data begins.
It therefore gives the TCP header length.
Just like IPv4 IHL, TCP Data Offset is measured in:
Minimum TCP header
Maximum header
TCP Control Flags
TCP flags control important TCP actions.
The six classic flags commonly shown in university TCP-header diagrams are:
SYN
SYN is used during TCP connection establishment.
ACK
ACK indicates that the Acknowledgement Number field contains valid acknowledgement information.
FIN
FIN tells the peer that this endpoint has finished sending data in that direction.
RST
RST immediately resets or rejects a TCP connection.
PSH
PSH is a delivery hint associated with pushing received data toward the application instead of unnecessarily waiting for more buffering.
URG
URG indicates that the Urgent Pointer field is meaningful.
TCP Window Size
The Window Size field is used for TCP flow control.
The receiver tells the sender how much additional receive space it can accept.
The base Window field is 16 bits.
The TCP Window Scale option can be negotiated to support much larger effective receive windows.
TCP Checksum
The TCP Checksum is 16 bits.
It is used to detect corruption affecting the TCP segment.
The calculation covers more than just the visible TCP header.
Source, destination and protocol-related information
Urgent Pointer
The Urgent Pointer field contains 16 bits.
It is interpreted when the URG control flag is active.
Urgent-data mechanisms are uncommon in many modern applications, but the field remains important for TCP-header questions.
TCP Options and Padding
TCP can include optional information after the fixed header fields.
Padding is added when necessary so the TCP header ends on a 32-bit boundary.
TCP Is a Byte-Stream Protocol
TCP does not preserve application message boundaries.
If an application writes:
then 200 bytes
the receiving application should not assume TCP will necessarily deliver those bytes as exactly two separate reads of 100 and 200 bytes.
TCP presents an ordered stream of bytes.
How TCP Provides Reliability
Track byte positions.
Confirm received byte ranges.
Detect likely loss.
Resend missing data.
Duplicate Detection
Sequence numbers also help TCP identify duplicate data.
Out-of-Order Data
TCP can hold received data while waiting for missing earlier bytes so the application receives the stream in order.
Retransmission Timeout
If acknowledgement for outstanding data does not arrive within the calculated retransmission timeout, TCP may retransmit the missing data.
Fast Retransmission
Repeated duplicate acknowledgements can indicate that a segment was lost even before the retransmission timer expires.
Traditional explanations often describe retransmission after three duplicate ACKs, although real modern TCP implementations use more detailed algorithms.
TCP Flow Control
The receiver may not be able to process incoming data as quickly as the sender can generate it.
TCP therefore uses receiver-advertised window information.
If receiver buffer space becomes smaller, the advertised receive window can decrease.
TCP Flow Control vs Congestion Control
Flow Control
Protects the receiver.
Uses receiver-advertised capacity such as rwnd.
Congestion Control
Protects the network from excessive traffic.
TCP maintains congestion-related sending limits such as a congestion window.
Congestion Control = Network problem
TCP MSS and MTU
MSS means:
MSS describes TCP payload size, not the complete IP packet size.
Common IPv4 Example
Suppose:
TCP Connection Management
TCP is connection-oriented.
Before normal data transfer, both endpoints create and synchronize connection state.
Connection management has three major parts:
- Connection establishment
- Data transfer
- Connection termination
TCP Three-Way Handshake
TCP normally establishes a connection using three control exchanges.
Let:
Server Initial Sequence Number = y
then SYN-SENT
then SYN-RECEIVED
Why Does TCP Need a Three-Way Handshake?
Because both sides must confirm each other's initial sequence information and verify two-way communication state. The final ACK confirms that the client received the server's SYN information.
Solved TCP Handshake Sequence Example
Suppose:
Step 1
Step 2
Step 3
TCP Data Transfer After the Handshake
After both endpoints reach the ESTABLISHED state, application data can flow.
TCP uses:
- Sequence numbers
- Acknowledgement numbers
- Receive windows
- Checksums
- Retransmission timers
- Congestion-control algorithms
Simple Data Example
Suppose a TCP segment begins with:
The data covers bytes:
The receiver can acknowledge:
TCP Connection Termination
TCP provides full-duplex communication.
This means each direction can be closed separately.
A common orderly TCP close therefore uses four control exchanges.
Active Closer
Passive Closer
Step 1 - First FIN
Host A says:
Step 2 - ACK
Host B acknowledges Host A's FIN.
Step 3 - Second FIN
When Host B has also finished sending, it sends its own FIN.
Step 4 - Final ACK
Host A acknowledges Host B's FIN.
TCP Half-Close
Because TCP is full duplex, one endpoint can finish sending while still receiving data from the other side.
For example:
Host A cannot send more application data after that direction closes.
Host B may still send remaining data until it also sends FIN.
This condition is called a:
Why Does TCP Use TIME-WAIT?
The endpoint that actively closes a TCP connection normally enters the TIME-WAIT state after sending the final ACK.
TIME-WAIT helps old delayed segments disappear before the same connection identification is reused.
It also lets the endpoint send the final ACK again if the peer retransmits its FIN.
TCP RST - Resetting a Connection
Examples can include:
- Connecting to a TCP port where no service is listening.
- Aborting an existing TCP connection.
- Rejecting invalid TCP state.
RST: Abrupt reset.
Important TCP Connection States
TCP maintains state because it is a connection-oriented protocol.
Common Server States
Other Important States
- CLOSE-WAIT
- LAST-ACK
- CLOSING
TCP vs UDP
| Point | TCP | UDP |
|---|---|---|
| Full Form | Transmission Control Protocol | User Datagram Protocol |
| Connection | Connection-oriented | Connectionless |
| Reliability | Provides reliability mechanisms | No TCP-style reliability |
| Ordering | Ordered byte stream | Datagrams remain separate |
| Minimum Header | 20 bytes | 8 bytes |
| Handshake | Connection setup required | No TCP three-way handshake |
| Sequence Numbers | Yes | No TCP sequence-number mechanism |
| ACK | Used for reliable delivery | No TCP-style ACK system |
| Flow Control | Yes | No TCP-style receive-window flow control |
| Common Uses | Web, file transfer, email and reliable streams | DNS, streaming scenarios, gaming and applications needing low overhead |
Solved TCP Numericals
Numerical 1: TCP Header Size
Given:
Numerical 2: Next ACK Number
A TCP segment has:
Data bytes are:
Next expected byte:
Numerical 3: SYN Acknowledgement
Client sends:
Server acknowledges:
Numerical 4: FIN Acknowledgement
Suppose FIN uses:
The peer acknowledges:
Numerical 5: Common TCP MSS
Numerical 6: Maximum Basic Window Field
The basic TCP Window field contains:
Numerical 7: Handshake Sequence Numbers
Given:
The connection-establishment numbers are:
TCP Remember Tricks
Congestion Control: Protect network.
Common TCP Exam Mistakes
Wrong. TCP is connection-oriented.
Wrong. 20 bytes is the minimum. TCP options can increase the header size up to 60 bytes.
Wrong. TCP sequence numbers are related to byte positions in the TCP stream.
TCP ACK normally indicates the next byte expected.
Wrong. SYN consumes one sequence number.
Wrong. FIN also consumes one sequence number.
Wrong. Normal TCP connection establishment uses a three-way handshake.
Because TCP is full duplex, each direction normally closes separately, commonly resulting in FIN, ACK, FIN, ACK exchanges.
Wrong. Flow control protects the receiver; congestion control responds to network congestion.
Wrong. MSS refers to TCP payload size.
Wrong. TCP provides a byte stream.
Wrong. TCP checksum covers TCP header and TCP data and uses an IP pseudo header in its calculation.
Wrong. FIN is used for orderly shutdown, while RST is used for an abrupt reset.
Wrong. TIME-WAIT is associated with connection shutdown and protection against delayed old segments.
40 Important TCP Questions With Answers
20 TCP Protocol MCQs With Answers
A. Transfer Control Process
B. Transmission Control Protocol
C. Transport Communication Process
D. Transmission Channel Protocol
A. Connectionless
B. Connection-oriented
C. Data Link protocol
D. Routing algorithm
A. 8 bytes
B. 16 bytes
C. 20 bytes
D. 40 bytes
A. 40 bytes
B. 60 bytes
C. 64 bytes
D. 128 bytes
A. 8 bits
B. 16 bits
C. 32 bits
D. 64 bits
A. 8 bits
B. 16 bits
C. 32 bits
D. 128 bits
A. Previous packet number only
B. Next byte expected
C. Router address
D. MAC address
A. FIN
B. SYN
C. RST
D. URG
A. SYN, SYN-ACK, ACK
B. FIN, ACK, FIN
C. ACK, ACK, ACK
D. SYN, FIN, RST
A. No sequence number
B. One sequence number
C. Two sequence numbers
D. 32 sequence numbers
A. Connection termination
B. Routing
C. Fragmentation
D. DNS resolution
A. Reset connection
B. Reliable Segment Transfer
C. Routing State Table
D. Receiver Sequence Timer
A. IP address size
B. TCP header length
C. Ethernet frame length
D. Number of routers
A. 5 bytes
B. 10 bytes
C. 20 bytes
D. 40 bytes
A. Flow control information
B. DNS mapping
C. IP routing
D. MAC addressing
A. Optional in TCP
B. Used for error detection
C. Used only for routing
D. Same as TTL
A. Message-boundary preservation
B. Ordered byte stream
C. Only multicast
D. Connectionless datagrams
A. ESTABLISHED
B. CLOSED only
C. LISTEN only
D. TIME-WAIT only
A. SYN, SYN, ACK
B. FIN, ACK, FIN, ACK
C. RST only always
D. UDP ACK
A. 1400 bytes
B. 1440 bytes
C. 1460 bytes
D. 1500 bytes
Frequently Asked Questions
What is TCP in simple words?
TCP is a Transport Layer protocol that creates a connection and provides reliable, ordered byte-stream communication between applications.
What is a TCP segment?
A TCP segment contains a TCP header and optional application data.
What is the minimum TCP header size?
The minimum TCP header size is 20 bytes.
What is the maximum TCP header size?
The maximum TCP header size is 60 bytes.
What does the TCP Sequence Number mean?
It normally identifies the number of the first data byte carried by a TCP segment.
What does the TCP ACK number mean?
It normally indicates the next byte the receiver expects.
What is the TCP three-way handshake?
TCP connection establishment normally uses SYN, SYN-ACK and ACK.
Why does SYN increase acknowledgement by one?
Because SYN consumes one TCP sequence number.
How does TCP terminate a connection?
A normal full-duplex connection close commonly uses FIN, ACK, FIN and ACK exchanges because each direction closes independently.
What is TCP TIME-WAIT?
TIME-WAIT is a connection-closing state that helps delayed old TCP segments expire and allows the final ACK to be retransmitted if the peer repeats its FIN.
What is the difference between FIN and RST?
FIN closes one sending direction in an orderly manner, while RST abruptly resets the TCP connection.
What is TCP MSS?
MSS is Maximum Segment Size and normally represents the maximum TCP payload size an endpoint advertises it can receive.
Does TCP preserve message boundaries?
No. TCP provides an ordered byte stream, so application message boundaries are not preserved automatically.
What is TCP flow control?
TCP flow control uses the receiver's advertised window to help prevent a sender from overflowing the receiver's buffer.
