Skip to content
Sign inGet all access
Chapter 4·8 minFree

Carrying Data

ACKs, windows, and why TCP is a stream and not messages.

After the handshake, your bytes.

A data packet

[P.] and a non-zero length:

cap "-i lo -n -c 5 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000" | tail -2
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:4, ack 1, win W, length 3IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [.], ack 4, win W, length 0

Three bytes out, an acknowledgement back carrying none.

ACKs

ack 4 means “I have everything up to byte 4, send that one next”. Six bytes gets ack 7:

cap "-i lo -n -c 5 tcp port 9000" "printf 'abcdef' | nc -w 1 127.0.0.1 9000" \
  | tail -2
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:7, ack 1, win W, length 6IP 127.0.0.1.9000 > 127.0.0.1.CLIENT: Flags [.], ack 7, win W, length 0

Every byte is acknowledged, and anything unacknowledged is sent again.

Relative numbers

seq 1:4 is bytes 1 up to but not including 4, counted from this connection’s start.

cap "-i lo -n -c 4 tcp port 9000" "echo hi | nc -w 1 127.0.0.1 9000" | tail -1
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:4, ack 1, win W, length 3

tcpdump does the subtraction. On the wire it is chapter 3’s random start plus one, which -S shows as ten digits.

The receive window

win is how many more bytes this side will accept — flow control in one number:

terminal
tcpdump -i lo -n -c 3 'tcp port 9000' > /tmp/w.txt 2>/dev/null &
echo hi | nc -w 1 127.0.0.1 9000
grep -oE 'win [0-9]+' /tmp/w.txt | tr '\n' ' '
Outputwin 65495 win 65483 win 512

This course’s cap helper prints win W because the value differs between runs.

MSS

The largest segment each side will accept, announced in the SYN:

terminal
tcpdump -i lo -n -c 1 'tcp port 9000' > /tmp/m.txt 2>/dev/null &
nc -w 1 127.0.0.1 9000 </dev/null
grep -oE 'mss [0-9]+' /tmp/m.txt
Outputmss 65495

65495 because loopback has no wire. Across a network it is about 1460 — chapter 6 explains why.

More than one packet

Anything over the MSS is split. 200,000 bytes takes nine data packets:

terminal
head -c 200000 /dev/zero | tr '\0' 'x' > /tmp/big.txt
cap "-i lo -n -c 20 tcp port 9000" \
    "nc -w 2 127.0.0.1 9000 < /tmp/big.txt" | grep -c 'P\.'
Output9

Those nine arrive alongside eleven packets carrying nothing but acknowledgements:

terminal
cap "-i lo -n -c 20 tcp port 9000" \
    "nc -w 2 127.0.0.1 9000 < /tmp/big.txt" | grep -c 'length 0'
Output11

A stream, not messages

Three separate writes, with pauses, send three packets:

terminal
cap "-i lo -n -c 7 tcp port 9000" \
    "{ printf a; sleep 0.1; printf b; sleep 0.1; printf c; } \
     | nc -w 1 127.0.0.1 9000" | grep 'P\.'
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:2, ack 1, win W, length 1IP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 2:3, ack 1, win W, length 1

The same three bytes written at once send one:

cap "-i lo -n -c 6 tcp port 9000" "printf 'abc' | nc -w 1 127.0.0.1 9000" \
  | grep 'P\.'
OutputIP 127.0.0.1.CLIENT > 127.0.0.1.9000: Flags [P.], seq 1:4, ack 1, win W, length 3

Every protocol above has to frame its own messages — HTTP uses Content-Length or chunked encoding, and this is why.