Transport Layer (TCP)
Lecture 5 | CSE421 – Computer Networks
Department of Computer Science and Engineering
School of Data & Science
Objectives
• TCP Header
• TCP Services
TCP Segment Header
24.3
Byte Number
• The bytes of data being transferred in each connection are
numbered by TCP.
• The numbering starts with an arbitrarily generated number.
• An arbitrary number between 0 and 232 − 1 for the number of
the first byte.
• For example if the number of the first byte happens to be 1067
and the total data to be sent is 3000 bytes .
• What is the byte number for the first byte of data and last byte
of data?
First Byte Number Last Byte Number
1067 4066
Sequence Numbers
• The sequence number of the first segment is the ISN (initial sequence number),
which is a random number (byte number).
• The sequence number of any other segment is the sequence number of the previous
segment plus the number of bytes (real or imaginary) carried by the previous
segment.
• Suppose a TCP connection is transferring a file of 5,000 bytes. The first byte is
numbered 10,001. What are the sequence numbers for each segment if data are sent
in five segments, each carrying 1,000 bytes?
• Solution:
Acknowledgement Number
• If receiving host TCP receives uncorrupted data, then…
• It is acknowledged using the acknowledgement number
• The value of the acknowledgment field in a segment defines the
number of the next byte the receiver expects to receive.
• For example if the sender receives 1001 as the acknowledgement
number.
• What does it mean?
Received all data up to Note :This does not indicate
1000, tells the sender that it ready receiver has received 1000 bytes
to receive the next data from 1001 of data.
byte number.
Acknowledgement Number
Expectational Acknowledgement
Acknowledgement Number
• The acknowledgment number is cumulative.
• Receiver acknowledges multiple data segments in one
acknowledgement.
Sender
Receiver
Header Length
• Header Length :
▪ Indicates the number of 4-byte words
▪ The length of the header can be between 20 and 60 bytes
Control Bits
• Control Bits:
▪ This field defines 6 different control bits or flags
▪ One or more of these bits can be set at a time
▪ These bits help indicate connection establishment and termination,
flow control
Window Size
• Window Size:
▪ This field defines size of data in bytes of the sending TCP process
▪ The maximum size of the window is 65,535 bytes
▪ Normally referred to as the receiving window (rwnd )
▪ The sender must obey the dictation of the receiver in this case
Checksum
• This 16 bits field is used to detect errors (i.e., flipped bits) in the transmitted
segment (intentionally or unintentionally) while traveling through the network.
UDP HEADER
• Also present in UDP header
• Mandatory in TCP but not in UDP
• Process is same for both protocols
▪ TCP/UDP Header
▪ TCP/UDP Body
▪ Pseudo IP Header
Urgent Pointer
• This 16-bit field, which is valid only if the urgent flag is set.
• Used when the segment contains urgent data.
• It defines a value that must be added to the sequence number to
obtain the number of the last urgent byte in the data section of
the segment.
1500
1oo1 1001+1500
Options
▪ There can be up to 40 bytes of optional information in the TCP
header
▪ Provides a way to deal with limitations of the original header
▪ For example :
• MSS (Maximum Segment Size) is defined as the largest block of
data that a sender using TCP will send to the receiver
•Primary
Functions of the Transport Layer
responsibilities:
1. Segmenting the data and managing each piece.
2. Reassembling the segments into streams of
application data.
UDP &TCP 3. Identifying the different applications.
4. Multiplexing
Only TCP
5. Establishing and terminating a connection.
6. Enabling error control and recovery.
7. Performing flow control between end users. 15
• Function 6
Connection Establishment and
Termination for Reliability
Connection Establishment
• TCP sets up a connection between end hosts
before sending data
• This process is known as ”Three-way
handshake”
• After the connection is established the hosts can
send data
3 Way Handshake : Connection Establishment
49152
80
Source Port No. Destination Port No.
Sequence No.
Acknowledgement No.
U A P R S F Window Size
Others
49152
49152
80 80 49152
80
100 0 101
0 101 1
A S 4000 Bytes 8000 Bytes
Others
3 Way Handshake : Connection Establishment
24.19
Data Transfer
Data Transfer Continued..
3 Way Handshake : Connection Termination
49152 80
Source Port No. Destination Port No.
Sequence No.
Acknowledgement No.
U A P R S F Window Size
Others
49152 80 80 49152
10001 17001 10002
17002
17001 10002
A F 4000 Bytes 8000 Bytes
Others
Connection Termination :: Half Close
• Function 6
Error Control and Recovery for Reliability
Reliability in TCP
• TCP provides reliability using error control
• Error control includes mechanisms for
▪ detecting and resending corrupted segments
▪ resending lost segments
▪ storing out-of order segments until missing segments arrive
▪ detecting and discarding duplicated segments.
• Error control in TCP is achieved through
▪ Checksum
▪ Acknowledgment
▪ Time-out and retransmission
• Checksum Error Control
▪ Each segment includes a checksum field, which is used to check for a
corrupted segment
▪ If a segment is corrupted, as detected by an invalid checksum, the
segment is discarded
• Acknowledgment
▪ Using Acknowledgement Number to confirm the receipt of data
segments.
▪ To confirm control segments that carry no data, but consume a
sequence number
▪ ACK segments do not consume sequence numbers and are not
acknowledged.
Error Control
• Retransmission
• When a segment is sent, it is stored in a queue until it is acknowledged.
• Retransmission of segment will occur
• After Retransmission Time Out
• The sending TCP maintains one retransmission time-out (RTO) timer for each
connection.
• When the timer matures TCP resends the segment in the front of the queue if
the segment is not acknowledged
• After Three Duplicate ACK Segments
Seq – 1
Start 20
Ack – 4 1
001 [Da
ta 200 byte
s]
Start Stop
Seq – 4001
Ack – 1401 [Data 1000
bytes] Normal
Operation
500 ms
Time Out Seq – 1401
Ack – 5001
[no new data is se
nt. so sequence nu
the next segment mber of
does not change]
tes]
Seq –
5001
a t a 1000 by
401 [ D
Ack - 1
Start
es]
q – 6 001 t a 1 0 00 byt
Se [Da
401
Ack- 1
< 500 ms
Seq – 1401
Ack - 7001
Other Scenarios
• Segment Lost or Corrupted?
• Retransmission of segment ??
• How will the sender know ??
• What about the receiver, not aware of a packet sent?
• RTO - Retransmission after time out.
Start
Seq – 7001 tes]
– 1 4 0 1 [Data 1000 by
Buffer Ack
1
Seq – 800 [Data 1000 bytes]
- 1 4 0 1 < 500 ms
Ack
Seq – 1401
Ack - 9001
Stop Start
001 ytes]
Seq – 9 1 [Data 1000 b
4 0
Ack – 1
01
Seq – 100 [Data 1000 bytes]
- 1 4 0 1
Buffer Ack
Seq – 1401
Ack - 9001 Time Out
tes]
9001 000 by
Seq – 01 [Data 1
4
Ack- 1 Time out & Restart Timer
Buffer
Seq – 1401
Ack - 1100
1
Stop
Lost Segment
Out of Order Segments
• TCP implementations today do not discard out-of-order
segments.
• They store them temporarily .
• Flag them as out-of-order segments until the missing
segments arrive.
• Out-of-order segments are never delivered to the
process.
• TCP guarantees that data are delivered to the process in
order.
Lost ACK 1 Start
Seq – 1100 bytes]
1 4 0 1 [Data 1000
Ack –
Buffer
Seq – 1401
Ack - 12001
Time out & Restart Timer
tes]
q – 11001 ata 1000 by
Se [D
1401 Seq – 1401
Ack – Ack - 12001
Stop
Start
tes]
12001 000 by
Seq – 01 [Data 1
4
Ack- 1
]
3001 0 bytes Time Out
Seq – 1 1 [Data 100
40
Buffer Ack- 1
tes]
14001 000 by
Seq – 01 [Data 1
4
Ack- 1
] Seq – 1401
tes
15001 000 by Ack - 1200
Seq – 01 [Data 1 1
4
Ack- 1
Seq – 140
Ack - 120
1
01
1
3 ACKs – Fast
Seq – 1401
Ack - 12001
2
Transmission
3
Seq – 1401
001 bytes]
Seq – 12 [D ata 1000 Ack - 12001
4 0 1 Time Out Timer
Ack- 1
• Function 7 :
Flow Control and Recovery for
Reliability
Flow Control
• Transmission Control Protocol (TCP) uses a sliding window for flow
control.
• What is the “Window ”?
▪ Indicates the size of the device's receive buffer for the particular
connection.
▪ How much data a device can handle from its peer at one time before it is
passed to the application process.
▪ Set by receiver of data
▪ Example : The server's window size was 360. This means the
receiver is willing to take no more than 360 bytes at a time
from the sender.
Sender Sliding Window
Window Size = 100 bytes
First Variables
outstanding Next byte to
byte send Sf = 201
Sn Sn = 261
Sf
... 200 201 ..... 260 261 ..... 300 301 ...
Bytes that are Outstanding Bytes Bytes that can be sent Bytes that cannot
acknowledged sent but not acknowledged (Usable window) be sent
Window Size as advertised by the receiver
Sliding of Sender Window
Sliding of Sender Window
#Sender receives a segment with ACK 241
And rwnd Window Size = 100 bytes
rwnd= =60100 First First Next byte to
outstanding outstanding send Variables Variables
byte byte
Sf = 201 Sf = 241
Sf Sn Sn = 261 Sn = 261
Sf
... 200 201 ... 241 242 ... 260 261 ..... 300 301 ... 340
Outstanding Bytes Bytes that cannot
Bytes that are Outstanding Bytes Bytes that can be sent be sent
Bytessent
thatbut
arenot acknowledged
sent but not acknowledged
acknowledged (Usable window)
acknowledged
Window Size as advertised by the receiver
Receiver Sliding Window
Window Size = 100 bytes
Next byte to Next byte
be pulled by expected to
process receive
Rn
... 200 201 ..... 260 261 ..... 300 301 ...
Bytes that have
Bytes received and Bytes that can be Bytes that cannot
already been pulled
acknowledged, waiting to received from the sender be received
by the process
be consumed by process Receive window(rwnd)
Receive window and allocated buffer
• rwnd= buffer size – number of bytes to be pulled = 40 bytes
Sliding of Receiver Window
Flow Control Example
Flow Control Example Contd
Different TCP Sliding Window Protocols
• Selective Repeat Protocol
▪ Only those segments are
re-transmitted which are found
lost or corrupted
▪ Keep track of out of order
segments at the receiver side
▪ More efficient for noisy channels
▪ Widely used in TCP
Go Back N ARQ
Start
Seq – 7001 tes]
– 1 4 0 1 [Data 1000 by
Buffer Ack
1
Seq – 800 [Data 1000 bytes]
- 1 4 0 1 < 500 ms
Ack
Seq – 1401
Ack - 9001
Stop Start
001 ytes]
Seq – 9 1 [Data 1000 b
4 0
Ack – 1
01
Seq – 100 [Data 1000 bytes]
- 1 4 0 1
Buffer Ack
Seq – 1401
Ack - 9001 Time Out
tes]
9001 000 by
Seq – 01 [Data 1
4
Ack- 1 Time out & Restart Timer
Buffer
01
Seq – 100 [Data 1000 bytes]
- 1 4 0 1
Ack
Seq – 1401
Ack - 1100
1
Different TCP Sliding Window Protocols
• Go Back N Protocol
▪ If the sent segment are are found corrupted or
lost then all the segments are re-transmitted
from the lost segment to the last segment
transmitted
▪ Do not keep track of out of order segments
▪ Efficient for less noisy channel
Overall Flow control
• The initial window size is agreed during the three-way
handshake.
• If this is too much for the receiver and it loses data (e.g.
buffer overflow) then it can decrease the window size.
• If all is well then the receiver will increase the window
size.
The End