TCP (Transmission Control Protocol) is a connection-oriented transport layer protocol in the TCP/IP suite that provides reliable, ordered, and error-checked delivery of a stream of octets between applications. Unlike connectionless protocols, it requires an established session before any payload data moves across the network. This makes it the foundational transport mechanism for web browsing, file transfers, email, and remote administration.
How it works
Before exchanging application data, TCP establishes a synchronized session using a three-way handshake:
- SYN: The client sends a synchronize packet with an initial sequence number to the server.
- SYN-ACK: The server responds with its own sequence number and acknowledges the client's request.
- ACK: The client confirms receipt, moving both endpoints into an established state.
During transmission, TCP breaks data into segments and assigns a sequence number to each byte. The receiver sends back an acknowledgment (ACK) for successfully received segments and uses checksums to detect corruption. If packets arrive out of order, the receiving network stack reassembles them before passing the stream to the application. If an acknowledgment does not arrive within a calculated timeout window, TCP triggers a retransmission.
To maintain network stability, TCP manages data rates through two mechanisms: flow control (using a sliding window to prevent overwhelming the receiver's memory buffer) and congestion control (adjusting transmission speed based on perceived network bottlenecks). A standard TCP header is 20 bytes long—larger than UDP's 8-byte header—because it carries sequence numbers, acknowledgment flags, and window size metadata.
Example
System administrators routinely inspect active TCP connections and listening ports on Linux servers using the ss (socket statistics) utility. Running the following command displays all listening TCP sockets alongside the processes bound to them:
ss -tlpn
In the output, a line showing LISTEN 0 4096 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=1142,fd=6)) confirms that the Nginx web server is actively waiting for incoming TCP handshakes on port 443 with a connection backlog queue of 4096.
What matters when choosing a server or hosting
When configuring virtual private servers, dedicated hardware, or cloud load balancers, several TCP-level factors directly impact performance:
- Physical latency: Because every new TCP connection requires a round-trip handshake before sending data, server proximity to end users significantly affects initial connection times.
- Congestion control algorithms: Modern Linux kernels allow switching congestion control modules (such as CUBIC or BBR) via
sysctl, which affects throughput over high-latency or lossy links. - Connection limits and tuning: High-traffic web servers require adequate file descriptors, tuned
net.core.somaxconnbacklog limits, and proper ephemeral port ranges to avoid dropping incoming handshakes during traffic spikes. - Firewall state tracking: Stateful firewalls must track every open TCP session in memory; under heavy concurrent load or SYN flood attacks, undersized connection tracking tables can drop legitimate traffic.
Common mistakes
- Ignoring TIME_WAIT accumulation: Rapidly opening and closing short-lived TCP connections (such as unpooled database queries) leaves sockets in the
TIME_WAITstate, eventually exhausting available local ports. - Blocking Path MTU Discovery: Dropping all ICMP packets at the firewall prevents TCP endpoints from negotiating optimal Maximum Segment Sizes, leading to silent packet fragmentation or stalled transfers.
- Misconfiguring keepalives: Relying on default operating system TCP keepalive timers—often set to two hours—allows intermediate NAT gateways or load balancers to silently drop idle connections without notifying the application.
FAQ
When should I use TCP instead of UDP?
Use TCP whenever complete, ordered data delivery is mandatory, such as for HTTPS APIs, SSH sessions, database replication, and file transfers. Choose UDP for real-time audio, video streaming, or online gaming where occasional packet loss is preferable to retransmission delays.
What is TCP head-of-line blocking?
Because TCP guarantees strictly ordered delivery to the application, a single lost packet stalls the entire byte stream in the receive buffer until that specific segment is retransmitted, delaying subsequent packets that have already arrived safely.
How does TCP terminate a connection?
A graceful shutdown typically uses a four-way handshake involving FIN (finish) and ACK packets so each side can close its data stream independently, or an immediate RST (reset) packet when an error or abrupt termination occurs.