TCP

TCP (Transmission Control Protocol) is a core protocol of the Internet Protocol Suite along-side IP, creating the TCP/IP transmission standard. Where IP handled addressing and routing, TCP handles the data exchange between hosts. TCP provides reliable, handshaked, guaranteed data transfer, but at the expense of speed. If speed is valued over data reliability, UDP offers a faster alternative.

Structure
TCP Header

Protocols
TCP segments consist of a header part, and a data part.

The header contains 11 fields: 10 mandatory, 1 optional.

 Source Port (16 bits) - port sending the data Destination Port (16 bits) - port receiving the data Sequence Number (32 bits) - If the SYN flag is set this is the sequence number. The first data byte is this number plus 1. This the SYN flag is not set, this is the total sequence number Acknowledgment Number (32 bits) - Ascts as a receipt. If the ACK flag is set, this is the next expected sequence number. Data Offset (4 bits) - Specifies the size of the header in 32-bit chunks from 5 to 15. Reserved (4 bits) - not used, set to zero Flags (8 bits) - 8 flags

 CWR (1 bit) - Congestion Window Reduced flag: indicates that an ECE flag was received and is being handled</li> ECE (1 bit) - ECN-Echo: If SYN is set, then the peer is ECN capable. If SYN is not set, a packet marked has experiencing congestion was received.</li> URG (1 bit) - Urgent: Urgent pointer field is important</li> ACK (1 bit) - Acknowledgment : Acknowledgment is important, all subsequent packets should have ACK set. Disabling this prevents data verification</li> PSH (1 bit) - Push: push the buffered data to the destination client</li> RST (1 bit) - Reset: reset the connection</li> SYN (1 bit) - Synchronize: Only set on the first packet. Synchronizes the sequence numbers</li> FIN (1 bit) - Finished: Sender is done, no more packets will be sent.</li> </ul> </li> Window (16 bits) - size of receive window which specifies the number of bytes the receiver is willing to accept.</li> Checksum (16 bits) - 16-bit checksum to verify received data</li> Urgent Pointer (16 bits) - If URG is set, this is a 16-bit offset from the sequence number indicating the last urgent byte.</li> Options (variable, up to 120 bits, divisible by 8	<ul>		0 (8 bits) - End of list</li>		1 (8 bits) - no padding</li>		2,4,SS (32 bits) - max segment size (must have SYN set)</li>		3,3,S (24 bits) - window scale(must have SYN set)</li>		4,2 (16 bits) - selective acknowledgment (must have SYN set)</li>		<li>5,N,BBBB,EEEE,... (variable bits, N is either 10, 18, 26, or 34) - Selective ACK (SACK)</li>		<li>8,10,TTTT,EEEE (80 bits) - Timestamp and echo previous timestamp</li>		<li>14,3,S (24 bits) - Request alternate checksum (must have SYN set)</li>		<li>15,N,... (variable bits) - Alternate checksum</li>	</ul> </li> </ol>

TCP takes place in three phases: connection establishment, data transfer, and connection termination.

TCP also goes between several different state changes throughout its lifespan:

<ol> <li>LISTEN - Wait for a connection request.</li> <li>SYN-SENT - Wait for the peer to send back SYN and ACK</li> <li>SYN-RECEIVED - Wait for the remote peer to send a confirmation ACK after initial ACK response.</li> <li>ESTABLISHED - Port ready.</li> <li>FIN-WAIT-1</li> <li>FIN-WAIT-2</li> <li>CLOSE-WAIT</li> <li>CLOSING</li> <li>LAST-ACK</li> <li>TIME-WAIT - Wait to allow time for the peer to receive the ACK (max 4 minutes)</li> <li>CLOSED - End connection</li> </ol>

IPv4
As defined by RCF 793.

TCP pseudo-header (IPv4)

IPv6
As defined by RCF 2360.

TCP pseudo-header (IPv6)

Links

 * RFC 793 - TCPv4
 * RFC 1122 - Error corrections for TCPv4