What is TCP?
TCP turns the internet's best-effort packet delivery into a reliable stream of bytes. It numbers every byte, waits for acknowledgments, resends what goes missing and puts everything back in order before the app sees it.
Web pages, email, file transfers, SSH and most app traffic ride on TCP. Knowing the handshake, sequence numbers and the difference between FIN and RST explains most slow or broken connections you will ever debug.
Think of it like this. TCP is like sending a book by post one numbered page at a time. The reader posts back a card saying which page they need next. If page 12 never arrives, they keep asking for page 12, and you send it again. They only read once every page is in order.
Step by step
Normal
4 stepsOpen a connection, download a small file in order, then close politely.
Normal
4 stepsOpen a connection, download a small file in order, then close politely.
- 1
Three-way handshake
SYN, SYN-ACK, ACK
Before any data moves, both sides pick a random starting sequence number and confirm the other's. They also agree on the largest segment size (MSS) and whether selective acknowledgments are allowed. It costs one round trip.
- 2
Request
The laptop sends 110 bytes
The app hands TCP a request. TCP numbers the bytes 1 to 110 and sends them. The server answers with an ACK that says: I have everything up to byte 110, send 111 next.
- 3
Data and ACKs
Segments arrive and are confirmed
The server sends the file in segments of up to 1460 bytes. The laptop does not need to confirm each one: a single ACK for the highest byte in order covers all before it. The window field tells the sender how much room the receiver has left.
- 4
Graceful close
FIN in both directions
Each side sends a FIN when it has nothing more to say, and the other side confirms it. The side that closed first waits a while in TIME_WAIT, so late packets from this connection cannot confuse a new one.
Packet lost
5 stepsOne segment vanishes on the way. Duplicate ACKs warn the sender, and it resends just that piece.
Packet lost
5 stepsOne segment vanishes on the way. Duplicate ACKs warn the sender, and it resends just that piece.
- 1
Sending fast
Five segments in a row
The connection is already open. The server sends five segments of 1460 bytes back to back, as its congestion window allows.
- 2
One goes missing
Bytes 1461 to 2920 are lost
A busy router somewhere drops the second segment. The others still arrive, but now there is a gap. TCP never hands bytes to the app out of order, so it keeps the later ones waiting.
- 3
Duplicate ACKs
The laptop keeps asking for 1461
Every new segment that lands after the gap makes the laptop repeat its last ACK: I still need byte 1461. SACK blocks add: but I do have 2921 and up. Three duplicate ACKs are a strong sign of loss, not just reordering.
- 4
Fast retransmit
Resent without waiting for a timer
After the third duplicate ACK the server resends the missing segment at once instead of waiting for a timeout of a second or more. It also slows down, because loss usually means the path is congested.
- 5
Back in order
One ACK covers everything
When the missing piece lands, the laptop can release all 7300 bytes to the app in order and jumps its ACK forward to 7301.
Reset
4 stepsA closed port refuses at once with RST, and a restarted server kills an old connection the same way.
Reset
4 stepsA closed port refuses at once with RST, and a restarted server kills an old connection the same way.
- 1
Knock on a closed port
SYN to port 8081
The laptop tries a port where no program is listening. The server's operating system still answers, because it owns the address.
- 2
Connection refused
RST, ACK comes back
RST means reset: there is no connection here, stop. Apps show this as connection refused. A firewall that silently drops the SYN instead gives a timeout, which is slower to notice.
- 3
Server restarts
An old connection is now unknown
Later, the laptop has a long-lived connection on port 8080. The server reboots and loses its memory of it. The laptop does not know yet.
- 4
Reset by peer
Data meets RST
When the laptop sends more data, the server has no matching connection and answers RST. The app sees connection reset by peer and must reconnect. Unlike FIN, RST is abrupt: data still in flight is thrown away.
Myths and mix-ups
- TCP does not guarantee delivery no matter what; it guarantees you will know when delivery failed.
- TCP does not send one ACK per packet; one ACK can confirm many segments at once.
- A refused connection (RST) and a timeout are different: refused means the host answered, timeout often means a firewall dropped it.
- Sequence numbers do not start at 0; they start at a random value, and tools show them relative to make them readable.
- TCP is not always slower than UDP for bulk data; for large downloads it is usually the right choice.
Try it yourself
Run these on your own computer to see TCP at work.
List open connections (Windows)
netstat -ano -p tcpShows each connection's local and remote address, state such as ESTABLISHED or TIME_WAIT, and the process ID.
List open connections (Linux)
ss -tan-t for TCP, -a for all states, -n for numbers. Look for LISTEN, ESTAB and TIME-WAIT.
List open connections (macOS)
netstat -an -p tcpThe State column shows where each connection is in its life.
Test if a port is open (macOS or Linux)
nc -vz example.com 443succeeded means the handshake worked; refused means an RST came back.
Test if a port is open (Windows)
Test-NetConnection example.com -Port 443Run in PowerShell. TcpTestSucceeded : True means the handshake worked.
TCP at a glance
- Layer
- Transport (4)
- IP protocol number
- 6
- Header size
- 20 to 60 bytes
- Ports
- 0 to 65535, picked by the app
- Connection setup
- Three-way handshake (SYN, SYN-ACK, ACK)
- Typical MSS
- 1460 bytes on Ethernet
- First standard
- 1981 (RFC 793)
- Current standard
- RFC 9293 (2022)
Defined inRFC 9293RFC 5681RFC 2018RFC 7323RFC 6298
Header fields
What TCP puts in its messages, with example values from the stories above.
TCP
- Source port
51000A short-lived port the laptop picked for this one connection.- Destination port
8080- Sequence number
0 (relative)Counts the bytes this side has sent. The real number starts at a random value; tools show it from 0 to keep it readable.- Header length
32 bytes- Flags
SYNSYN means synchronise: let us agree on starting numbers.- Window
64240How many more bytes this side can take right now. This is flow control.- Checksum
verified- Options
MSS=1460, SACK permitted, window scale 8MSS is the largest piece of data per segment. SACK lets the receiver report gaps exactly.- Acknowledgment number
1 (relative)Ack=1 because a SYN counts as one byte, so byte 1 is next.- Payload length
110 bytes
Test yourself
What are the three packets of the TCP handshake?
Questions people ask
What is the TCP three-way handshake?
Three packets that open a connection: SYN from the client, SYN-ACK from the server and ACK from the client. They agree on starting sequence numbers and options like the maximum segment size.
What does connection reset by peer mean?
The other side sent a TCP RST. It no longer knows about your connection, often because the program crashed, the server restarted or a firewall dropped its record. Reconnecting usually fixes it.
What is TIME_WAIT in TCP?
The state the side that closed first stays in for a short time after the close, often up to a minute or two depending on the system. It makes sure stray packets from the old connection cannot be mistaken for a new one.
What is the difference between TCP and UDP?
TCP sets up a connection and makes sure every byte arrives in order, resending what is lost. UDP just sends single messages with no setup and no resending, which is faster but leaves reliability to the app.
Why are some TCP packets retransmitted?
A segment was lost or its ACK did not come back in time. The sender resends it after a timeout, or sooner after three duplicate ACKs. Many retransmissions usually mean congestion or a weak Wi-Fi link.