Are you an LLM? You can read better optimized documentation at /en\wiki\TCP.md for this page in Markdown format
TCP
TCP (Transmission Control Protocol) is one of the core protocols of the internet: it defines how data is moved reliably across a network. It sits at the transport layer, between the application layer and the network layer, and provides a reliable, connection-oriented service.
Characteristics
- Reliability: data arrives in order and without errors, enforced with acknowledgements (ACK), retransmission and checksums.
- Connection-oriented: a connection is established before communication — the three-way handshake — and torn down afterwards with the four-way handshake.
- Flow control: a sliding window prevents the sender from overwhelming the receiver.
- Congestion control: TCP detects congestion and backs off. The classic algorithms are slow start, congestion avoidance, fast retransmit and fast recovery.
- Ordered delivery: segments arrive in the order they were sent, even if the network splits or reorders them.
- Error detection: checksums detect corruption, and recovery restores integrity.
Packet layout
A TCP segment carries these main fields:
- Source port: the sender's port.
- Destination port: the receiver's port.
- Sequence number: the position of this segment in the stream.
- Acknowledgment number: the next byte expected, confirming what has been received.
- Data offset: the length of the TCP header.
- Flags: control bits such as SYN, ACK, FIN, RST, PSH and URG.
- Window size: how much the receiver can accept, used for flow control.
- Checksum: detects errors in the segment.
- Urgent pointer: marks the position of urgent data.
- Options: optional extensions for newer features.
Connection management
Three-way handshake (opening)
- SYN: the client sends a SYN (synchronise sequence number) to request a connection.
- SYN-ACK: the server replies with SYN and ACK, agreeing to connect and acknowledging the client's SYN.
- ACK: the client acknowledges the server's SYN-ACK, and the connection is open.
Four-way handshake (closing)
- FIN: the client sends FIN to signal it wants to close.
- ACK: the server acknowledges the FIN.
- FIN: the server sends its own FIN.
- ACK: the client acknowledges that FIN, and the connection is closed.
Use cases
TCP is used wherever reliable delivery matters:
- Web browsing: HTTP/HTTPS run over TCP.
- Email: SMTP, IMAP and POP3 run over TCP.
- File transfer: FTP runs over TCP.
- Remote login: SSH and Telnet run over TCP.
Trade-offs
Advantages
- Reliable delivery with guaranteed order and integrity.
- Flow and congestion control that adapt to the network.
- Connection-oriented, which suits long, stable conversations.
Drawbacks
- More overhead and lower efficiency than connectionless UDP.
- Opening and closing a connection adds latency, which hurts latency-sensitive applications.
Summary
TCP is one of the most important protocols on the internet: reliable, connection-oriented, and used by most network applications. Flow control, congestion control and error detection keep it dependable on difficult networks. It costs some overhead and latency, but its stability is why it remains the default choice.
