# Transmission Control Protocol (TCP)
## What is TCP
TCP, the Transmission Control Protocol, is a connection-oriented transport layer protocol that provides reliable, ordered, and error-checked delivery of data between applications running on networked devices. TCP is one of the two most important protocols in the internet protocol suite, the other being IP. Together they form TCP/IP, the foundation of internet communication. Applications that cannot tolerate data loss, such as web browsers, email clients, and file transfer applications, use TCP.
## TCP Characteristics
Connection-oriented means that TCP establishes a logical connection between the sender and receiver before any data is exchanged. This connection setup involves a handshaking process that ensures both sides are ready to communicate and agrees on parameters for the communication. Reliable delivery means that TCP guarantees every byte of data sent will be received by the other end. It achieves this through acknowledgments, timeouts, and retransmission. Ordered delivery means that data arriving at the destination is delivered to the application in the same order it was sent, even if individual segments arrive out of order. Error checking means that TCP includes a checksum to detect data corruption.
## TCP Segment
TCP sends data in units called segments. Each segment consists of a TCP header followed by the data payload. The header contains the source port, destination port, sequence number, acknowledgment number, flags, window size, checksum, and other fields. The sequence number identifies the position of the first byte of data in the segment relative to the beginning of the data stream. The acknowledgment number contains the sequence number of the next byte the sender expects to receive, acknowledging all bytes received up to that point.
## TCP Connection Lifecycle
A TCP connection goes through several states during its lifetime. It begins in the CLOSED state. After the three-way handshake, both sides are in the ESTABLISHED state and data can flow. When either side initiates termination, both sides go through a series of FIN and ACK exchanges before reaching the CLOSED state again. The TIME_WAIT state ensures that delayed packets from the closed connection are not mistaken for packets belonging to a new connection using the same port numbers.
## TCP Reliability Mechanisms
TCP uses several mechanisms to provide reliability. Sequence numbers allow the receiver to detect missing, duplicate, or out-of-order segments. Acknowledgments tell the sender which data has been successfully received. Retransmission timers trigger retransmission of segments when acknowledgments are not received within the expected time. Selective acknowledgments, enabled by the SACK option, allow the receiver to inform the sender exactly which data has been received, allowing only the missing segments to be retransmitted.Back to Subject