CSC 458/2209 – Computer Networking Systems
Lecture 7: Transmission Control
Protocol (TCP)
Peter Marbach & Sajad Shirali-Shahreza
Department of Computer Science
University of Toronto
Transmission Control Protocol (TCP)
Connection oriented
Explicit set-up and tear-down of TCP session
Stream-of-bytes service
Sends and receives a stream of bytes, not messages
Reliable, in-order delivery
Checksums to detect corrupted data
Acknowledgments & retransmissions for reliable delivery
Sequence numbers to detect losses and reorder data
Flow control
Prevent overflow of the receiver’s buffer space
Congestion control
Adapt to network congestion for the greater good
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
TCP “Stream of Bytes” Service
Host A Byte 0
Byte 1
Byte 2
Byte 3
Byte 80
Host B
Byte 0
Byte 1
Byte 2
Byte 3
Byte 80
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
…Emulated Using TCP “Segments”
Host A
Byte 0
Byte 1
Byte 2
Byte 3
Byte 80
Segment sent when:
TCP Data 1. Segment full (Max Segment Size),
2. Not full, but times out, or
3. “Pushed” by application.
TCP Data
Host B
Byte 0
Byte 1
Byte 2
Byte 3
Byte 80
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
TCP Segment
IP Data
TCP Data (segment) TCP Hdr IP Hdr
TCP Header
TCP header is typically 20 bytes long
TCP Segment
No more than Maximum Segment Size (MSS) bytes
E.g., up to 1460 consecutive bytes from the stream
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Sequence Numbers
Host A
ISN (initial sequence number)
Sequence TCP
TCP Data
number = 1st HDR
byte ACK sequence
number = next
expected byte
TCP
TCP Data HDR
Host B
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Initial Sequence Number (ISN)
Sequence number for the very first byte
E.g., Why not a de facto ISN of 0?
Practical issue
IP addresses and port #s uniquely identify a connection
Eventually, though, these port #s do get used again
… and there is a chance an old packet is still in flight
… and might be associated with the new connection
So, TCP requires changing the ISN over time
Set from a 32-bit clock that ticks every 4 microseconds
… which only wraps around once every 4.55 hours!
But, this means the hosts need to exchange ISNs
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
TCP Three-Way Handshake
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Establishing a TCP Connection
A B
SYN
S YN A
CK Each host tells
its ISN to the
AC K other host.
Data
Data
Three-way handshake to establish connection
Host A sends a SYN (open) to the host B
Host B returns a SYN acknowledgment (SYN ACK)
Host A sends an ACK to acknowledge the SYN ACK
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
TCP Header
Source port Destination port
Sequence number
Flags: SYN
Acknowledgment
FIN
RST HdrLe 0 Flags Advertised window
PSH n
URG Checksum Urgent pointer
ACK Options (variable)
Data
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Step 1: A’s Initial SYN Packet
A’s port B’s port
A’s Initial Sequence Number
Flags: SYN
Acknowledgment
FIN
RST 20 0 Flags Advertised window
PSH
URG Checksum Urgent pointer
ACK Options (variable)
A tells B it wants to open a connection…
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Step 2: B’s SYN-ACK Packet
B’s port A’s port
B’s Initial Sequence Number
Flags: SYN
A’s ISN plus 1
FIN
RST 20 0 Flags Advertised window
PSH
URG Checksum Urgent pointer
ACK Options (variable)
B tells A it accepts, and is ready to hear the next byte…
… upon receiving this packet, A can start sending data
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Step 3: A’s ACK of the SYN-ACK
A’s port B’s port
Sequence number
Flags: SYN
B’s ISN plus 1
FIN
RST 20 0 Flags Advertised window
PSH
URG Checksum Urgent pointer
ACK Options (variable)
A tells B it is okay to start sending
… upon receiving this packet, B can start sending data
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
What if the SYN Packet Gets Lost?
Suppose the SYN packet gets lost
Packet is lost inside the network, or
Server rejects the packet (e.g., listen queue is full)
Eventually, no SYN-ACK arrives
Sender sets a timer and wait for the SYN-ACK
… and retransmits the SYN if needed
How should the TCP sender set the timer?
Sender has no idea how far away the receiver is
Hard to guess a reasonable length of time to wait
Some TCPs use a default of 3 or 6 seconds
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Tearing Down the Connection
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Tearing Down the Connection
B
SYN A
FIN A
ACK
ACK
ACK
SYN
FI N
FI N
Data
C
CK
K
A
time
Closing the connection
Finish (FIN) to close and receive remaining bytes
And other host sends a FIN ACK to acknowledge
Reset (RST) to close and not receive remaining bytes
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019
Sending/Receiving the FIN Packet
Sending a FIN: close() Receiving a FIN: EOF
Process is done sending Process is reading data
data via the socket from the socket
Process invokes “close()” Eventually, the attempt to
to close the socket read returns an EOF
Once TCP has sent all of
the outstanding bytes…
… then TCP sends a FIN
CSC 458/CSC 2209 – Computer Networks University of Toronto – Fall 2019