Introduction

IP delivers datagrams between hosts, unreliably. The transport layer turns this into communication between processes, identified by 16-bit port numbers, and optionally adds reliability. The Internet has two main transport protocols:

  • UDP adds only ports and a checksum to IP: no connection, no acknowledgement, no ordering, no flow or congestion control. Each send is one datagram. It suits short request-response exchanges (DNS), real-time media and protocols that manage their own reliability.
  • TCP gives a reliable, ordered, full-duplex byte stream over a connection: it numbers bytes, acknowledges them, retransmits lost data, and paces the sender to the receiver (flow control) and to the network (congestion control).

Both protocols are described by what they carry and by how a TCP connection is opened, used and closed: headers, sequence and acknowledgement numbers, the three-way handshake, connection release, the state machine and the retransmission timer. Flow control and congestion control have their own notes.

Prerequisites

Core concepts

Ports and demultiplexing

ProtocolA socket is identified by
Unconnected UDP (usual default)(destination IP, destination port): datagrams from any source to that endpoint reach the same socket; connected UDP records a peer and filters other source endpoints
TCP(source IP, source port, destination IP, destination port): each connection has its own socket, so one server port serves many clients

Well-known server ports: 20 and 21 FTP, 22 SSH, 23 Telnet, 25 SMTP, 53 DNS, 80 HTTP, 443 HTTPS.

UDP header and checksum

FieldBits
Source port16
Destination port16
Length (header + data, bytes; at least 8)16
Checksum16

The checksum is the Internet checksum over a pseudo-header (source IP, destination IP, a zero byte, protocol 17, UDP length), the UDP header (with checksum 0) and the data, padded with a zero byte if odd. Including the IP addresses guards against delivery to the wrong host. Over IPv4 the checksum is optional: a transmitted value of 0 means "not computed", so a computed result of 0 is sent as 0xFFFF.

TCP header

FieldBitsMeaning
Source port, destination port16 eachProcess endpoints
Sequence number32Number of the first data byte in this segment (or the ISN on a SYN)
Acknowledgement number32Next byte expected from the other side; valid when ACK = 1
Data offset4Header length in 4-byte words: 5 to 15, so 20 to 60 bytes
Reserved4Zero
Flags8CWR, ECE, URG, ACK, PSH, RST, SYN, FIN
Window16Bytes the sender of this segment is willing to receive (flow control)
Checksum16Internet checksum over pseudo-header (protocol 6), header and data; mandatory
Urgent pointer16Valid when URG = 1
Options0 to 320MSS (on SYN), window scale, SACK permitted, timestamps
The TCP header drawn as 32-bit rows with bit positions 0 to 31: row 1 Source Port 16 bits and Destination Port 16 bits; row 2 Sequence Number 32 bits; row 3 Acknowledgement Number 32 bits; row 4 Data Offset 4 bits, Reserved 4 bits, flags CWR ECE URG ACK PSH RST SYN FIN one bit each, Window 16 bits; row 5 Checksum 16 bits and Urgent Pointer 16 bits; then Options and padding, 0 to 40 bytes. A brace marks the first five rows as 20 bytes.
Figure 1. The TCP header: 20 bytes without options (data offset 5), with 32-bit sequence and acknowledgement numbers, eight one-bit flags and a 16-bit window.

Sequence and acknowledgement numbers

  • TCP numbers bytes, not segments. A segment with sequence number ss and LL data bytes carries bytes ss to s+L−1s + L - 1; the next segment starts at s+Ls + L.
  • The initial sequence number (ISN) is chosen at connection set-up, not fixed at 0, to keep segments from an old connection with the same ports from being taken as new.
  • SYN and FIN each consume one sequence number, even when they carry no data; any payload bytes consume their own sequence numbers as usual. Pure ACKs consume none.
  • Acknowledgements are cumulative: ACK number aa means every byte up to a−1a - 1 has arrived in order.

Connection establishment: the three-way handshake

  1. Client to server: SYN, seq = xx.
  2. Server to client: SYN + ACK, seq = yy, ack = x+1x + 1.
  3. Client to server: ACK, seq = x+1x + 1, ack = y+1y + 1 (it may already carry data).

Three messages are the minimum that let both sides learn that the other has received their ISN. A two-way exchange would leave the server unable to tell a fresh SYN from a delayed duplicate of an old one.

Connection release

Each direction is closed separately (a half-close): the side that has finished sends FIN, the other acknowledges it, and may keep sending until it sends its own FIN. The usual exchange is four segments, FIN, ACK, FIN, ACK, or three when the second FIN rides on the first ACK. The side that closes first waits in TIME-WAIT for 2×MSL2 \times \text{MSL} (maximum segment lifetime; RFC 9293 takes MSL as 2 minutes) before forgetting the connection, so that its last ACK can be resent if lost and old duplicates die out.

Timeline between a client on the left and a server on the right, time running down. Opening: SYN seq 2000; SYN+ACK seq 7000 ack 2001; ACK seq 2001 ack 7001. Data: client sends 1000 bytes seq 2001, then 500 bytes seq 3001; server replies ack 3501 and sends 300 bytes seq 7001; client acks 7301. Release: client FIN seq 3501; server ACK ack 3502; server FIN seq 7301; client ACK ack 7302, then the client waits in TIME-WAIT for 2 MSL. State names appear beside each side: client CLOSED, SYN-SENT, ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, TIME-WAIT; server LISTEN, SYN-RECEIVED, ESTABLISHED, CLOSE-WAIT, LAST-ACK, CLOSED.
Figure 2. A whole TCP connection: handshake with ISNs 2000 and 7000, 1500 client bytes and 300 server bytes, and a four-segment release ending with the client in TIME-WAIT.

The state machine

State diagram of a TCP connection with the states CLOSED, LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSING, TIME-WAIT, CLOSE-WAIT and LAST-ACK, labelled with the event and the segment sent on each transition.
Figure 3. The TCP connection state machine, a simplified form of RFC 9293 Figure 5 (main transitions only): eleven states, with the active closer passing through FIN-WAIT-1, FIN-WAIT-2 or CLOSING, and TIME-WAIT.

Eleven states. The active closer passes through FIN-WAIT-1, FIN-WAIT-2 (or CLOSING on a simultaneous close) and TIME-WAIT; the passive closer through CLOSE-WAIT and LAST-ACK. SYN-SENT to SYN-RECEIVED is the simultaneous open, where both sides send SYNs at once.

The retransmission timer

TCP retransmits a segment if its acknowledgement does not arrive within the retransmission timeout (RTO), estimated from measured round-trip times RR (RFC 6298):

  • first measurement: SRTT=R\text{SRTT} = R, RTTVAR=R/2\text{RTTVAR} = R/2;
  • later measurements, RTTVAR first, using the old SRTT: RTTVAR←34 RTTVAR+14 ∣SRTT−R∣\text{RTTVAR} \leftarrow \tfrac34\,\text{RTTVAR} + \tfrac14\,|\text{SRTT} - R|, then SRTT←78 SRTT+18 R\text{SRTT} \leftarrow \tfrac78\,\text{SRTT} + \tfrac18\,R;
  • RTO=SRTT+max⁡(G,4 RTTVAR)\text{RTO} = \text{SRTT} + \max(G, 4\,\text{RTTVAR}), with clock granularity GG, rounded up to at least 1 second.

Karn's algorithm: do not take RTT samples from retransmitted segments (the ACK could belong to either copy), and double the RTO after each timeout until a new sample is taken. The original TCP specification (RFC 793) used only a weighted average, SRTT←α SRTT+(1−α)R\text{SRTT} \leftarrow \alpha\,\text{SRTT} + (1 - \alpha) R with α\alpha about 0.8 to 0.9, and RTO=β×SRTT\text{RTO} = \beta \times \text{SRTT} with β\beta about 1.3 to 2.0 (within fixed bounds); a problem must state which rule and which constants it uses.

Mathematical explanation: counting bytes

For a connection with client ISN xx and server ISN yy, after the client has sent DcD_c data bytes and the server DsD_s:

client’s next seq=x+1+Dc,server’s next seq=y+1+Ds,\text{client's next seq} = x + 1 + D_c, \qquad \text{server's next seq} = y + 1 + D_s,

and the client's FIN carries seq x+1+Dcx + 1 + D_c and is acknowledged with x+2+Dcx + 2 + D_c. The +1+1 terms are the SYN and the FIN.

Header overheads. With no options, a TCP segment over IPv4 carries 40 bytes of headers (20 IP + 20 TCP), a UDP datagram 28 (20 + 8). The maximum segment size advertised over Ethernet is usually 1500−40=14601500 - 40 = 1460 bytes.

Worked examples

Example 1: a whole connection

Client ISN 2000, server ISN 7000 (Figure 2).

#DirectionFlagsSeqAckData bytes
1C to SSYN2000n/a0
2S to CSYN, ACK700020010
3C to SACK200170010
4C to SACK200170011000 (2001 to 3000)
5C to SACK30017001500 (3001 to 3500)
6S to CACK70013501300 (7001 to 7300)
7C to SACK350173010
8C to SFIN, ACK350173010
9S to CACK730135020
10S to CFIN, ACK730135020
11C to SACK350273020

Segments 3, 7 and 11 are pure ACKs and do not advance the sequence number; the FINs at 3501 and 7301 do.

Example 2: UDP checksum

A UDP datagram from 192.168.10.5:5353 to 172.16.30.40:53 carries the 4 bytes "GATE". Length = 12. The 16-bit words summed are:

PartWords
Pseudo-headerC0A8 0A05 AC10 1E28 0011 000C
UDP header (checksum 0)14E9 0035 000C 0000
Data "GATE"4741 5445

The one's complement sum is 0x45B4, so the checksum is 0xBA4B.

Example 3: RTO estimation

RTT samples 100 ms then 140 ms; ignore GG and the 1-second minimum unless a problem includes them.

SampleRTTVARSRTTRTO
100 (first)50100100+4⋅50=300100 + 4 \cdot 50 = 300
14034⋅50+14⋅40=47.5\tfrac34 \cdot 50 + \tfrac14 \cdot 40 = 47.578⋅100+18⋅140=105\tfrac78 \cdot 100 + \tfrac18 \cdot 140 = 105105+190=295105 + 190 = 295

Using the new SRTT (105) in the RTTVAR update would give 34⋅50+14⋅35=46.25\tfrac34 \cdot 50 + \tfrac14 \cdot 35 = 46.25 and RTO 290: the order matters.

Example 4: data offset

A TCP header has data offset 8: header length 32 bytes, 12 of them options. If the IP total length is 552 with a 20-byte IP header, the segment carries 552−20−32=500552 - 20 - 32 = 500 data bytes.

Example 5: the original RFC 793 estimator

α=0.9\alpha = 0.9, β=2\beta = 2, SRTT starting at 30 ms, then samples of 26, 32 and 24 ms. SRTT becomes 0.9×30+0.1×26=29.60.9 \times 30 + 0.1 \times 26 = 29.6, then 0.9×29.6+3.2=29.840.9 \times 29.6 + 3.2 = 29.84, then 0.9×29.84+2.4=29.2560.9 \times 29.84 + 2.4 = 29.256 ms, and RTO =2×29.256=58.512= 2 \times 29.256 = 58.512 ms.

Example 6: Karn's rule and backoff

Continue Example 3 (SRTT 105, RTTVAR 47.5, RTO 295 ms). The next segment times out, so RTO doubles to 590 ms, and after a second timeout to 1180 ms. The ACK that finally arrives, 400 ms after the first copy was sent, could belong to either copy, so it gives no sample: SRTT and RTTVAR stay at 105 and 47.5. The following segment is acknowledged after 120 ms without retransmission:

RTTVAR=34⋅47.5+14⋅∣105−120∣=39.375,SRTT=78⋅105+18⋅120=106.875,RTO=106.875+4⋅39.375=264.375 ms.\text{RTTVAR} = \tfrac34 \cdot 47.5 + \tfrac14 \cdot |105 - 120| = 39.375, \quad \text{SRTT} = \tfrac78 \cdot 105 + \tfrac18 \cdot 120 = 106.875, \quad \text{RTO} = 106.875 + 4 \cdot 39.375 = 264.375 \text{ ms}.

Taking the ambiguous 400 ms as a sample would instead have given RTO 579.375 ms.

Example 7: a lost segment and the ACK numbers

Five 500-byte segments start at 1001, 1501, 2001, 2501 and 3001; the one starting at 1501 is lost, and the receiver buffers out-of-order data. The four segments that arrive draw the ACKs 1501, 1501, 1501 and 1501: the first acknowledges bytes 1001 to 1500, the next three are duplicates (which trigger fast retransmit, TCP Congestion Control). When the retransmitted segment 1501 arrives the gap is filled, and the ACK jumps to 3501, covering everything buffered.

Example 8: sequence numbers wrap

Client ISN 4,294,967,200. The first data byte is numbered 4,294,967,201; after 500 data bytes the next sequence number is (4,294,967,200+1+500) mod 232=405(4{,}294{,}967{,}200 + 1 + 500) \bmod 2^{32} = 405, and the 96th data byte carries number 0. All sequence arithmetic is modulo 2322^{32}.

Important properties and observations

PropertyUDPTCP
ConnectionNoneThree-way handshake, release
UnitDatagram (message boundaries kept)Byte stream (no boundaries)
Reliability, orderNoneRetransmission, cumulative ACKs, reordering
Flow and congestion controlNoneReceiver window; congestion window
Header8 bytes20 to 60 bytes
Checksum over IPv4Optional (0 = none)Mandatory

Common misconceptions.

  • "TCP sequence numbers count segments." They count bytes.
  • "The SYN's sequence number is the first data byte." The first data byte is ISN + 1.
  • "ACK number is the last byte received." It is the next byte expected.
  • "Pure ACKs consume a sequence number." Only SYN, FIN and data do.
  • "Connection release is three segments." Usually four; three only when FIN and ACK are combined.
  • "TIME-WAIT is on the server." It is on whichever side closes first.

GATE examination perspective

  • Seq/ack tracing. Start at ISN; add 1 for SYN and FIN, add LL for data; ACK = next expected byte.
  • State sequences. Active closer: ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, TIME-WAIT, CLOSED. Passive closer: ESTABLISHED, CLOSE-WAIT, LAST-ACK, CLOSED.
  • Header arithmetic. Data offset and IHL count 4-byte words; UDP length counts bytes.
  • Checksums. Pseudo-header with protocol 17 (UDP) or 6 (TCP).
  • RTO. RTTVAR before SRTT; RFC 6298 SRTT gain 1/81/8, RTTVAR gain 1/41/4; RTO =SRTT+4 RTTVAR= \text{SRTT} + 4\,\text{RTTVAR}; Karn's algorithm for retransmitted segments; RTO doubles on each timeout. With the RFC 793 rule, SRTT is a weighted average with weight α\alpha on the old SRTT, and RTO =β SRTT= \beta\,\text{SRTT}.
  • Lost segment. The ACK number stays at the first missing byte (duplicate ACKs) until the gap is filled, then jumps past everything buffered.

Summary

Sources

  1. A. S. Tanenbaum, D. J. Wetherall, Computer Networks, 5th ed., Pearson, 2011, Sections 6.2.2 (Connection Establishment), 6.2.3 (Connection Release), 6.4.1 (Introduction to UDP), 6.5.4 (The TCP Segment Header), 6.5.5 to 6.5.7 (connection establishment, release and management modelling) and 6.5.9 (TCP Timer Management).
  2. J. F. Kurose, K. W. Ross, Computer Networking: A Top-Down Approach, 8th ed., Pearson, 2021: Sections 3.3.1 (UDP Segment Structure), 3.3.2 (UDP Checksum), 3.5.2 (TCP Segment Structure), 3.5.3 (Round-Trip Time Estimation and Timeout) and 3.5.6 (TCP Connection Management).
  3. B. A. Forouzan, Data Communications and Networking, 5th ed., McGraw-Hill, 2013: Chapters 23 (Introduction to Transport Layer) and 24 (Internet Transport-Layer Protocols).
  4. J. Postel, RFC 768, User Datagram Protocol, 1980, (consulted 2026-11-11).
  5. J. Postel (Ed.), RFC 793, Transmission Control Protocol, 1981, Section 3.7 (Retransmission Timeout: the original estimator), (consulted 2026-11-11).
  6. W. Eddy (Ed.), RFC 9293, Transmission Control Protocol (TCP), 2022, Sections 3.1 (Header Format), 3.3.2 (State Machine Overview, Figure 5), 3.4 (Sequence Numbers), 3.5 (Establishing a Connection) and 3.6 (Closing a Connection), (consulted 2026-11-11).
  7. V. Paxson, M. Allman, J. Chu, M. Sargent, RFC 6298, Computing TCP's Retransmission Timer, 2011, Sections 2 (the estimator), 3 (Karn's algorithm) and 5 (timer management, backoff), (consulted 2026-11-11).

Next steps