LABS · PROTOCOLS · NET PATH

TCP: the reliable connection, segment by segment

TCP opens a connection, numbers every byte, acknowledges what it receives and resends what gets lost. Watch it in a real connection to a web server: the three-way handshake, the data, a retransmission and the close.

Level: beginner · 15 minutes

Interactive simulation

The animation needs JavaScript. The full lesson is written out below.

Download the capture and open it in Wireshark to keep exploring.

What TCP is

TCP (Transmission Control Protocol, RFC 9293) is the Internet's connection-oriented transport protocol. Before sending data, both ends agree through a three-way handshake. After that, TCP numbers every byte, acknowledges what it receives, resends what gets lost, delivers everything in order and paces itself so it doesn't overwhelm the receiver or the network.

It's used by applications that need everything to arrive, in order: the web (HTTP and HTTPS), email, SSH, file transfer and routing protocols such as BGP (port 179).

The TCP header

FieldBitsIn the laptop's GET
Source port1649152: ephemeral
Destination port1680: HTTP
Sequence number322840513217 (relative 1)
Acknowledgment number321730217649 (relative 1)
Header length48 32-bit words = 32 bytes
Flags8PSH and ACK
Window16502 (× 128 = 64,256 bytes)
Checksum16Header, data and a pseudo-header with the IPs
Urgent pointer160
Options0–320NOP, NOP and timestamps (12 bytes)

The header is 20 bytes without options and up to 60 with them. There are 4 reserved bits between the length and the flags. The flags are CWR, ECE, URG, ACK, PSH, RST, SYN and FIN.

How it works: a connection from start to finish

Seven steps: the three-way handshake, the data exchange, a lost segment, its retransmission and the close.

  1. SYN: the laptop asks to open the connection

    The browser wants to talk to 192.168.20.10:80. The laptop picks ephemeral port 49152 and a random initial sequence number (ISN), 2840513216, and sends a segment with the SYN flag and no data. In the options it announces what it can do: a 1460-byte MSS, SACK, timestamps and window scale 7. The connection is now SYN-SENT.

  2. SYN, ACK: the server accepts

    The server is listening on port 80, so it accepts. It picks its own ISN, 1730217648, and in the same segment acknowledges the laptop's SYN with Ack = laptop ISN + 1: the SYN counts as one byte even though it carries no data. It also announces its options. The server is now SYN-RECV, and the laptop moves to ESTABLISHED when it gets this segment.

  3. ACK: the connection is open

    The laptop acknowledges the server's SYN with Ack = 1730217649 (relative 1). This segment completes the three-way handshake and both ends are ESTABLISHED. From now on the window is multiplied by 128 (scale 7) and every segment carries timestamps; that's why the header is 32 bytes instead of 20.

  4. Data: every byte has its number

    The laptop sends GET / in a segment with 102 bytes of data (Seq=1). The server replies with 174 bytes of page (its Seq=1) and in the same segment acknowledges with Ack=103: the next byte it expects from the laptop. The laptop acknowledges the response with Ack=175 in a segment with no data. The PSH flag asks for the data to be handed to the application without waiting for more.

  5. A segment gets lost

    Four and a half seconds later, the laptop asks for /api/estado on the same connection: 119 bytes with Seq=103. R1's output queue toward the server is full and R1 drops it. Unlike UDP, the laptop will notice: it kept a copy of the segment and started the retransmission timer (RTO). If the acknowledgment doesn't arrive before it expires, it resends.

  6. Retransmission: the same bytes, again

    204 ms go by with no acknowledgment and the timer expires (in Linux, the minimum RTO is 200 ms plus the measured round-trip time). The laptop resends the segment with the same sequence number, 103; Wireshark flags it as TCP Retransmission. This time it gets through: the server replies with 97 bytes and Ack=222, and the laptop acknowledges with Ack=272. TCP takes the loss as a sign of congestion and shrinks its congestion window.

  7. FIN: closing in order

    Ana closes the tab. The laptop sends FIN: it won't send any more data (FIN-WAIT-1). The server acknowledges that FIN and, since it has nothing more to send either, closes its direction in the same segment. The laptop acknowledges with one last ACK and stays in TIME-WAIT: it waits 60 seconds in Linux (2 × MSL in the RFC) in case that ACK is lost and the server resends its FIN. Like the SYN, the FIN counts as one byte.

The three-way handshake

SegmentFlagsSeq (relative)Ack (relative)
Laptop → serverSYN0—
Server → laptopSYN, ACK01
Laptop → serverACK11

Each end picks its initial number (ISN) at random and the other acknowledges it by adding 1. The handshake also settles the options: the MSS, whether SACK is allowed, timestamps and window scale. An option is only used if both ends announced it.

Sequence, acknowledgments and retransmission

  • The sequence number is the position of the segment's first data byte in the stream.
  • The acknowledgment gives the next byte expected and confirms everything before it (cumulative acknowledgment).
  • If the acknowledgment doesn't arrive in time, the retransmission timer (RTO) expires and the segment is resent. RFC 6298 recommends a minimum of 1 second; Linux uses 200 ms.
  • After three duplicate acknowledgments, the sender resends without waiting for the RTO (fast retransmit). Linux can also resend earlier with a tail loss probe (TLP, RFC 8985).
  • With SACK (RFC 2018), the receiver says which blocks it got and the sender resends only what's missing.

Window and congestion control

The window is flow control: it says how many more bytes the receiver can take. With window scale (RFC 7323) it can go beyond 64 KB. The congestion window is computed by the sender: it grows while everything gets through and shrinks when there are losses. At any moment, the sender can have the smaller of the two outstanding.

The close and TIME-WAIT

StateWhat it means
FIN-WAIT-1Sent its FIN and waits for it to be acknowledged.
FIN-WAIT-2Its FIN was acknowledged; it waits for the other end's FIN.
CLOSE-WAITThe other end closed; the local application hasn't yet.
LAST-ACKSent its FIN after the other end and waits for the last ACK.
TIME-WAITWaits 2 × MSL (60 s in Linux) before releasing the connection.

The close usually takes four segments; here it took three because the server sent its ACK and its FIN together. An RST segment, on the other hand, cuts the connection abruptly, with no orderly close.

Seeing it on Cisco IOS XE

CommandWhat it shows or does
show tcp briefThe router's own TCP connections, such as SSH sessions.
show control-plane host open-portsThe TCP and UDP ports the device is listening on.
permit tcp any host 192.168.20.10 eq 80In an extended ACL, filters on the TCP destination port.
permit tcp any any establishedLets through segments with ACK or RST: return traffic for connections already open.
ip tcp adjust-mss 1360Lowers the MSS in SYNs crossing the interface, useful on tunnels.
show interfaces Gi0/0/2Output drops, where segments get lost.

What happens when it fails

  • Closed port: the server answers the SYN with an RST and the connection is refused at once.
  • A firewall that drops the SYN leaves the client retrying in SYN-SENT until it times out (in Linux, 6 retries, about 127 seconds).
  • Frequent losses: many retransmissions and a small congestion window make the connection slow.
  • MSS too large on a tunnel: big segments don't get through and the connection hangs after the handshake. Fix it with ip tcp adjust-mss.
  • Lots of connections in TIME-WAIT on a busy server: it's normal, but it uses up ephemeral ports.

Frequently asked questions

Why does the sequence number start at a random value?

So that an old segment from a previous connection isn't mistaken for a new one, and so an attacker can't guess it and inject data. Wireshark shows relative numbers, starting at 0, to make them easier to read.

What's the difference between MSS and MTU?

The MTU is the largest IP packet on the link: 1500 bytes on Ethernet. The MSS is the data portion of a TCP segment: 1500 minus 20 for IP and 20 for TCP, 1460 bytes. With timestamps, 1448 are left per segment.

Does the SYN carry data?

Normally not, but it uses up one sequence number; that's why the acknowledgment is the ISN plus 1. The same goes for the FIN.

Why does the laptop stay in TIME-WAIT and not the server?

Whoever closes first stays in TIME-WAIT. It waits in case its last ACK is lost and the other end resends the FIN, and so late segments don't get mixed into a new connection.

What is an RST?

A segment with the RST flag cuts the connection immediately, with no orderly close. It's sent, for example, when a SYN arrives at a port nobody is listening on.

Key takeaways

  • TCP is connection-oriented: a three-way handshake (SYN, SYN-ACK and ACK) comes before the data.
  • Every byte has a sequence number; the acknowledgment gives the next byte expected.
  • Whatever isn't acknowledged in time is resent: by RTO, by fast retransmit or with SACK.
  • The window controls how much the other end can receive; window scale allows more than 64 KB.
  • The close uses one FIN in each direction, and whoever closes first waits in TIME-WAIT.

NEXT STEP

From this lesson to the Canadá Logística network

In the CCNA 2.0 Course you filter TCP traffic with extended ACLs, analyze captures of slow connections and tune the MSS on Canadá Logística's tunnels, in Packet Tracer or CML. We'll let you know when it opens.

Upcoming labs

Sources

Lesson prepared with AI assistance and checked against the sources cited. The bytes of every packet are generated according to the standard and verified with an independent tool.