TCP

Transmission Control Protocol is a network communication protocol designed to reliably send ordered data packets over an IP network.

Segment Structure

A TCP segment consists of a TCP header and a data section. There are 10 mandatory fields for the header.

FieldDescriptionOffsetSize (Bits)
Source portThe sending port016
Destination portThe receiving port1616
Sequence numberThe initial sequence number (if SYN set) otherwise the accumulated sequence number3232
Acknowledgement numberIf ACK set, the expectant next sequence number6432
Data offsetSize of the TCP header in 32-bit words (min 5, max 15)964
ReservedFor future use (should be set to 000)1003
Flags9 1-bit flags1039
Window SizeThe size of the receive window11216
ChecksumError checking for the header12816
Urgent pointerIf URG is set, offset from the sequence number indicating the last urgent data byte14416
OptionsOptional header fields1600-320

The flags section indicates which fields are relevant in the header. There are 9 total flags.

FlagDescriptionTotal Offset
NSECN-nonce - concealment protection (experimental)103
CWRCongestion Window Reduced104
ECEIf SYN, the TCP peer is ECN capable, else congestion indicator105
URGIndicates the Urgent pointer field is significant106
ACKIndicates the Acknowledgement field is significant107
PSHAsks to push the buffered data to the receiving application108
RSTReset the connection109
SYNSynchronize sequence numbers110
FINLast packet from sender111

Protocol Operation

The protocol is divided into three phases: connection establishment, data transfer, and connection termination.

Connection Establishment

TCP uses a three-way handshake to establish a connection.

  1. SYN - The client sends SYN with a random sequence number x
  2. SYN-ACK - The server replies with SYN-ACK with an acknowledgement number x+1 and a random sequence number y
  3. ACK - The client sends ACK back to the server with an acknowledgement number y+1 and a sequence number x+1

Connection Termination

TCP uses a four-way handshake to terminate a connection. Each endpoint needs to send a FIN and a final ACK to terminate its side of the connection.

  1. FIN - The initiator sends FIN
  2. ACK - The receiver responds with ACK
  3. FIN - The receiver sends FIN
  4. ACK - The initiator responds with the final ACK

It is also possible to combine the receiver’s flags in steps 2 and 3 for a FIN-ACK.