TCP
Overview
- TCP stands for Transmission Control Protocol.
- It is a connection-oriented protocol in the Transport Layer of the OSI and TCP/IP models.
- TCP provides reliable, ordered, and error-checked delivery of data between applications running on hosts in a network.
- It is one of the core protocols of the Internet Protocol Suite (IP).
Key Features of TCP
-
Connection-Oriented:
- Before transmitting data, a connection must be established between the sender and receiver.
- This is done using a process known as the three-way handshake.
-
Reliable Communication:
- TCP ensures data delivery by using acknowledgements (ACKs) and retransmissions if data packets are lost.
-
Ordered Delivery:
- Data is delivered to the receiving application in the same order in which it was sent.
-
Error Detection and Recovery:
- TCP uses a checksum to detect errors.
- If errors are detected, TCP retransmits the affected data.
-
Full-Duplex Communication:
- TCP allows both sender and receiver to transmit data simultaneously.
TCP Header Structure
A TCP segment consists of a header and data. The TCP header is 20 to 60 bytes in size.
| Field | Size (Bits) | Description |
|---|---|---|
| Source Port | 16 | Port number of the sender |
| Destination Port | 16 | Port number of the receiver |
| Sequence Number | 32 | Order of the first byte of data in the segment |
| Acknowledgement Number | 32 | Next expected byte from the sender |
| Data Offset | 4 | Size of the TCP header (in 32-bit words) |
| Reserved | 3 | Reserved for future use (always 0) |
| Flags | 9 | Control flags (e.g., SYN, ACK, FIN) |
| Window Size | 16 | Size of the receive window |
| Checksum | 16 | Error-checking value |
| Urgent Pointer | 16 | Points to urgent data (if URG flag is set) |
| Options | Variable | Optional parameters (e.g., MSS, Window Scaling) |
| Data | Variable | The actual application data |
Flags in TCP Header:
- SYN: Initiates a connection.
- ACK: Acknowledges receipt of data.
- FIN: Gracefully terminates a connection.
- RST: Resets an existing connection.
- PSH: Requests immediate delivery of data to the application.
- URG: Indicates urgent data.
TCP Three-Way Handshake
The three-way handshake is used to establish a reliable connection between two devices.
-
SYN (Synchronize):
- The client sends a SYN packet to the server to initiate a connection.
- This packet contains a sequence number.
-
SYN-ACK (Synchronize + Acknowledge):
- The server responds with a SYN-ACK packet to acknowledge the client’s request.
- The packet contains the server's sequence number and an acknowledgment number.
-
ACK (Acknowledge):
- The client sends an ACK packet to confirm the connection.
Diagram:
Client Server
| SYN -> |
| <- SYN-ACK |
| ACK -> |
Connection Established!
TCP Connection Termination
To close a TCP connection, a four-step process is used:
- FIN Sent by Client: The client sends a FIN packet to indicate it has finished sending data.
- ACK Sent by Server: The server acknowledges the FIN.
- FIN Sent by Server: The server sends its own FIN to indicate it has finished sending data.
- ACK Sent by Client: The client acknowledges the server's FIN.
Diagram:
Client Server
| FIN -> |
| <- ACK |
| <- FIN |
| ACK -> |
Connection Closed!