How TCP Connections Work: Step-by-Step Guide
In this article
TCP (Transmission Control Protocol) is the backbone of most internet communication, ensuring data arrives reliably and in order. This guide explains the exact sequence of events—from the initial handshake to data transfer and connection teardown—so you can understand what happens behind the scenes when you load a webpage, send an email, or stream a video.
What You'll Need
- A basic understanding of IP addresses and ports
- Familiarity with terms like client, server, and packet
- Optional: Wireshark or tcpdump to observe real TCP traffic
Step-by-Step
- Client initiates a connection (SYN)
The client (your computer, phone, etc.) sends a TCP segment with the SYN (synchronize) flag set to the server's IP address and a specific port (e.g., port 80 for HTTP, 443 for HTTPS). This segment includes an initial sequence number (ISN), a random number that helps track bytes sent. - Server acknowledges and synchronizes (SYN-ACK)
If the server is listening on that port and accepts the connection, it replies with a segment that has both SYN and ACK (acknowledge) flags set. The ACK number is the client's ISN + 1, confirming receipt of the SYN. The server also sends its own ISN in the sequence number field. - Client acknowledges (ACK)
The client sends an ACK segment with the server's ISN + 1 in the acknowledgment number field. This completes the three-way handshake, and the connection is now established. Both sides can now send data. - Data transfer with sequence and acknowledgment numbers
Once established, data is sent in segments. Each byte has a sequence number. The receiver sends back ACKs with the next expected sequence number, indicating all bytes up to that point were received. If a segment is lost, the sender retransmits it after a timeout or upon receiving duplicate ACKs. - Flow control and congestion control
The receiver advertises a window size (in the TCP header) telling the sender how much data it can accept without overwhelming its buffer. The sender also adjusts its sending rate based on network congestion, using algorithms like slow start and congestion avoidance. - Connection teardown (FIN or RST)
When one side finishes sending data, it sends a FIN (finish) flag. The other side ACKs it, then sends its own FIN. Each FIN is acknowledged, and the connection closes gracefully. If an error occurs, a RST (reset) flag can abruptly terminate the connection.
Common Mistakes
- Confusing SYN-ACK with ACK – The SYN-ACK is a combined response to the initial SYN; the final ACK in the handshake is separate and has no SYN flag.
- Assuming TCP is connectionless – Unlike UDP, TCP requires a dedicated connection setup before data transfer.
- Misunderstanding sequence numbers – Sequence numbers represent byte positions, not packet counts. A packet with 1000 bytes increases the sequence number by 1000.
- Thinking the handshake is only for security – It also synchronizes sequence numbers, which is essential for reliable delivery.
Tips
- Use Wireshark to capture a TCP handshake—it's a great way to see the flags and numbers in action.
- Remember that ports are 16-bit numbers (0-65535). Ports below 1024 are well-known services; higher ports are often ephemeral.
- When troubleshooting, check if a firewall is blocking SYN packets—many firewalls treat unsolicited SYNs as suspicious.
- Understand that TCP ensures reliability but adds latency; for real-time apps like VoIP, UDP is often preferred.
Troubleshooting
- Connection times out – The SYN is sent but no SYN-ACK returns. Possible causes: server down, firewall blocking, or wrong port. Use
telnet host portornc -vz host portto test. - Connection resets (RST) – The server sends a RST, often because no service is listening on that port or the server is overloaded. Check the port and service status.
- Slow data transfer – Could be due to small window sizes, high latency, or packet loss. Use
pingandtracerouteto diagnose network issues. - Half-open connections – If one side crashes, the other may wait indefinitely. TCP keepalive probes can detect dead peers, but they are often disabled by default.
Frequently Asked Questions
What is the difference between TCP and UDP?
TCP is connection-oriented and guarantees reliable, ordered delivery with error checking and retransmission. UDP is connectionless, faster, but does not guarantee delivery or order. TCP is used for web, email, and file transfer; UDP for streaming, VoIP, and gaming.
Why does TCP use a three-way handshake?
The three-way handshake synchronizes sequence numbers between both sides, ensuring they agree on the starting byte count. It also prevents old or duplicate connection requests from being misinterpreted as new ones.
What happens if a TCP segment is lost?
The sender does not receive an ACK for that segment within the timeout period, so it retransmits it. The receiver may also send duplicate ACKs, triggering fast retransmission before the timeout.
Can a TCP connection exist without a handshake?
No. The handshake is mandatory for establishing a TCP connection. Without it, the sequence numbers are not synchronized, and reliable data transfer is impossible.
What is the maximum number of simultaneous TCP connections?
There is no hard limit in the protocol itself. The practical limit is determined by system resources (memory, file descriptors) and the server's configuration. A typical server can handle thousands to millions of connections.
How does TCP handle out-of-order segments?
The receiver buffers segments that arrive out of order and only delivers them to the application once the missing segments are received and the sequence is complete. The ACK number always reflects the next expected byte, not the last received byte.