Wiki DE Start in the browser

Wiki › Protocols

TCP

Reliable connections between programs: ports, three-way handshake, sequence and acknowledgement numbers, retransmission and teardown.

Layer
4 (Transport)
Standard
RFC 9293
See an example
See demo

Ports

IP brings a packet to the right device, ports to the right program. A web server waits on port 80, SSH on 22, SMTP on 25. The client chooses a free port from 49152. A connection is uniquely identified by source, destination and both ports.

Three-way handshake

StepFlagsMeaning
1SYNclient: “I want to connect, my initial number is x”
2SYN, ACKserver: “Agreed, mine is y, I expect x+1”
3ACKclient: “I expect y+1” – the connection is up

The TCP header

Without options 20 bytes. The first segment (SYN) usually carries the MSS option, then the header is 24 bytes long. Values from the HTTP request of the demo “DNS, TCP and HTTP”.

TCPSegment · RFC 9293
0481216202428
Source port16 bits49152
Destination port16 bits80
Sequence number32 bits11001
Acknowledgment number32 bits51001
Data offset4 bits5
Reserved4 bits0
Flags8 bitsACK, PSH
Window16 bits64240
Checksum16 bits0xeed9
Urgent pointer16 bits0
Options0–40 bytes
Datavariable100 Byte
Each row has 32 bits (4 bytes), bit 0 on the left, as the RFCs draw their headers. Dashed: variable or optional. Point at a field for an explanation.
FieldLengthExample valueMeaning
Source port16 bits (2 bytes)49152Port of the sending program, usually random from 49152 on the client.
Destination port16 bits (2 bytes)80Port of the service: 80 HTTP, 443 HTTPS, 22 SSH, 25 SMTP.
Sequence number32 bits (4 bytes)11001Number of the first data byte in this segment; with SYN the random initial number.
Acknowledgment number32 bits (4 bytes)51001The next byte expected (valid when ACK is set).
Data offset4 bits5Header length in 32-bit words: 5 = 20 bytes without options.
Reserved4 bits0Reserved, always 0.
Flags8 bitsACK, PSHSYN open, ACK acknowledgement valid, FIN close, RST abort, PSH push immediately, URG, ECE and CWR for congestion.
Window16 bits (2 bytes)64240How many bytes the other side may still send without acknowledgement (flow control).
Checksum16 bits (2 bytes)0xeed9Checksum over header, data and a pseudo header with the IP addresses.
Urgent pointer16 bits (2 bytes)0Only with URG: end of urgent data; hardly used today.
Options (optional)0–40 bytesE.g. MSS (maximum segment size), window scale, SACK, timestamps; mostly in the SYN.
Datavariable100 ByteThe application data, e.g. the HTTP request.
Values: Real frame from the demo “DNS, TCP and HTTP” (no. 36, 158 bytes), exactly as the packet list in leyoNET shows it.

Reliability

Every byte has a sequence number. The receiver acknowledges with the ACK number which byte it expects next. If the acknowledgement is missing, the sender retransmits. Large data is split into segments (MSS 1460 bytes on Ethernet). Teardown starts with a FIN, each side acknowledges; an RST aborts immediately, e.g. if nobody is waiting on the port.

Window and flow control

The TCP header contains the window: how many bytes the receiver can still take without every segment having to be acknowledged individually. The sender may have at most that much unacknowledged data on the way (RFC 9293, Section 3.8.6). If the receiver is overloaded, it reports a smaller window, down to 0: the sender waits. That is how TCP adapts to slow receivers (flow control). Against an overloaded network on the way, congestion control helps as well: if segments get lost, TCP sends more slowly.

In leyoNET

Open a web page in a PC’s browser, then filter the packet list with tcp. Clicking a packet and “Follow TCP stream” shows all segments of the connection. netstat on the command line lists open connections.

See an example

  • Demo “DNS, TCP and HTTP” in leyoNETDemo “DNS, TCP and HTTP” in leyoNET
    Slow motion This is how it looks in the simulation: every dot is a frame on the cable.
    DNS, TCP and HTTP

    A notebook fetches a web page through a router.

    DNS query for www.schule.example, TCP handshake (SYN, SYN-ACK, ACK) and the HTTP request in the browser.

    Watch live Project file

“Watch live” opens the network in leyoNET, the simulation runs in a loop. In the app you find it under File → Demo networks, with a step-by-step tour.

Practise

  • ★★★★☆ Protocol analysis – Task 3: ARP, DNS and HTTP · 7 goals · Worksheet (German)
  • ★★★☆☆ Open a web page via DNS · 3 goals · Worksheet (German)

In leyoNET under Tools → Exercise catalogue. Every exercise checks itself.

What leyoNET simplifies

ProtocolLayerSimplification in the simulation
TCP4Three-way handshake, sequence numbers in bytes, MSS 1460, retransmission after 1 s (at most three times), FIN/RST; fixed window size, no congestion control

Standards

  • RFC 9293 – Transmission Control Protocol (TCP)

Read on

Found a mistake or have an idea? Source of this page on GitHub.