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 -2Three 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 -2Every 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 -1tcpdump 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:
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' ' '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:
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.txt65495 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:
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\.'Those nine arrive alongside eleven packets carrying nothing but acknowledgements:
cap "-i lo -n -c 20 tcp port 9000" \
"nc -w 2 127.0.0.1 9000 < /tmp/big.txt" | grep -c 'length 0'A stream, not messages
Three separate writes, with pauses, send three packets:
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\.'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\.'Every protocol above has to frame its own messages — HTTP uses Content-Length or chunked
encoding, and this is why.