Advanced Computer Networks: MS-2 Semester
Advanced Computer Networks: MS-2 Semester
Lecture 05
(TCP)
MS- 2nd Semester
Instructor
Mr. Sadiq Shah
1
TCP
4
The TCP Connection
5
Three way handshake
6
TCP: Three Way HandShake
7
TCP and BUFFER
TCP directs this data to the connection’s send buffer, which is one
of the buffers that is set aside during the initial three-way
handshake.
From time to time, TCP will grab chunks of data from the send
buffer and pass the data to the network layer.
TCP pairs each chunk of client data with a TCP header, thereby
forming TCP segments.
The segments are passed down to the network layer, where they
are separately encapsulated within network-layer IP datagrams.
The IP datagrams are then sent into the network.
8
Sending and receiving buffers
When TCP receives a segment at the other end, the segment’s data is placed in
the TCP connection’s receive buffer.
Each side of the connection has its own send buffer and its own receive buffer
TCP segments
TCP segment structure
The TCP segment consists of header fields and a data field. The data field contains
a chunk of application data.
When TCP sends a large file, such as an image as part of a Web page, it typically
breaks the file into chunks of size MSSBecause the TCP header is typically 20 bytes
11
TCP segment structure
A TCP segment header also contains
the following fields:
1. 32-bit sequence number field
2. 32-bit acknowledgment number field
3. 16-bit receive window field is used for flow control
4. 4-bit header length field specifies the length of the TCP
header
5. The TCP header can be of variable length due to the TCP
options field.
6. The optional and variable-length options field is used
when a sender and receiver negotiate the maximum
segment size (MSS)
12
TCP segment structure
13
TCP segment structure
32 bits
URG: urgent data counting
source port # dest port #
(generally not used) by bytes
sequence number
ACK: ACK # of data
acknowledgement number (not segments!)
head not
PSH: push data now UA P R S F rcvr window size
len used
# bytes
(generally not used) checksum ptr urgent data
rcvr willing
RST, SYN, FIN: Options (variable length) to accept
connection estab(setup,
teardown commands)
application
Internet checksum data
(as in UDP) (variable length)
Sequence Numbers and Acknowledgment Numbers
16
The acknowledgment number that Host A puts in its segment
is the sequence number of the next byte Host A is expecting
from Host B.
It is good to look at a few examples to understand what is
going on here.
Suppose that Host A has received all bytes numbered 0
through 535 from B and suppose that it is about to send a
segment to Host B.
Host A is waiting for byte 536 and all the subsequent bytes in
Host B’s data stream.
So Host A puts 536 in the acknowledgment number field of
the segment it sends to B. 17
18
stop-and-wait operation
sender receiver
first packet bit transmitted, t = 0
last packet bit transmitted, t = L / R
U L/R 8ms
= = = 0.08
sender 100ms
D+L/R microsec
onds
1KB pkt every 100 ms -> 80Kbps throughput on a 1 Mbps link
What does bandwidth x delay product tell us?
Pipelined protocols
Pipelining: sender allows multiple, “in-flight”, yet-to-
be-acknowledged pkts
range of sequence numbers must be increased
buffering at sender and/or receiver
Increase utilization
by a factor of 3!
U 3*L/R 24ms
= = = 0.24
sender 100ms
D+L/R microsecon
ds
Go-Back-N
Allow up to N unACKed pkts in the network
N is the Window size
Sender Operation:
If window not full, transmit
ACKs are cumulative
On timeout, send all packets previously sent but
not yet ACKed.
Uses a single timer – represents the oldest
transmitted, but not yet ACKed pkt
Go-Back-N
Sender:
k-bit seq # in pkt header
“window” of up to N, consecutive unack’ed pkts allowed
N=4
Selective Repeat
receiver individually acknowledges all correctly
received pkts
buffers pkts, as needed, for eventual in-order delivery
to upper layer
sender only resends pkts for which ACK not
received
sender timer for each unACKed pkt
sender window
N consecutive seq #’s
again limits seq #s of sent, unACKed pkts
Selective repeat: sender, receiver windows
Selective repeat
sender receiver
data from above : pkt n
if next available seq # in send ACK(n)
window, send pkt out-of-order: buffer
receiver sees no
difference in two
scenarios!
incorrectly passes
duplicate data as new in
(a)
Q: what relationship
between seq # size and
window size?
TCP seq. #’s and ACKs
Seq. #’s:
Host A Host B
byte stream
“number” of first User Seq=4
2, AC
types K=79,
byte in segment’s data =
‘C’
‘C’
data host ACKs
receipt of
ACKs: = ‘C’
43 , data ‘C’, echoes
seq # of next byte CK=
eq = 79, A back ‘C’
S
expected from
other side host ACKs
cumulative ACK receipt Seq=4
3, ACK
of echoed =80
‘C’
time
simple telnet scenario
TCP: retransmission scenarios
Host A Host B Host A Host B
Seq=9 Seq=9
2, 8 b 2, 8 byte
yt e s d s data
ata Seq=
Seq=92 timeout
1 00, 2
0 byt
es da
Seq=100 timeout
t
timeout
a
=100
A CK 0
10
X CK =
A ACK =120
loss
Seq=9 Seq=9
2, 8 b 2, 8 b
y y te s d a
te s d a ta
ta
= 120
K
=100 AC
ACK
time time
lost ACK scenario premature timeout,
cumulative ACKs
TCP: third retransmission scenario
Cumulative
acknowledgement
avoids retransmission
of first segment
TCP Flow Control
flow control receiver: explicitly
sender won’t overrun informs sender of
receiver’s buffers by (dynamically changing)
amount of free buffer
transmitting too space
much,
rcvr window size
too fast field in TCP segment
sender: amount of
transmitted, unACKed
data less than most
recently-received rcvr
window size
receiver buffering
TCP Round Trip Time and Timeout
Q: how to set TCP Q: how to estimate RTT?
timeout value? SampleRTT: measured time from
longer than RTT segment transmission until ACK
receipt
note: RTT will vary
ignore retransmissions,
too short: premature
cumulatively ACKed segments
timeout
SampleRTT will vary, want
unnecessary
estimated RTT “smoother”
retransmissions
use several recent
too long: slow reaction
measurements, not just
to segment loss
current SampleRTT
TCP Round Trip Time and Timeout
EstimatedRTT = (1-x)*EstimatedRTT + x*SampleRTT
typical value of x: 0.1
timed wait
ACK
replies with ACK. Closes
connection, sends FIN.
closed
TCP Connection Management (cont.)
timed wait
ACK
closed
closed
TCP Connection Management (cont)
TCP client
lifecycle
TCP server
lifecycle