0% found this document useful (0 votes)
21 views

Advanced Computer Networks: MS-2 Semester

TCP is a connection-oriented protocol that creates virtual connections between endpoints to send data reliably. It uses mechanisms like flow control, congestion control, and error control. TCP connections involve a three-way handshake and use sequence numbers and acknowledgment numbers with segments. Pipelining allows sending multiple unacknowledged packets to improve link utilization.

Uploaded by

Amir Butt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Advanced Computer Networks: MS-2 Semester

TCP is a connection-oriented protocol that creates virtual connections between endpoints to send data reliably. It uses mechanisms like flow control, congestion control, and error control. TCP connections involve a three-way handshake and use sequence numbers and acknowledgment numbers with segments. Pipelining allows sending multiple unacknowledged packets to improve link utilization.

Uploaded by

Amir Butt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

Advanced Computer Networks

Lecture 05
(TCP)
MS- 2nd Semester

Instructor
Mr. Sadiq Shah
1
TCP

TCP is a connection-oriented protocol; it creates a


virtual connection between two TCPs to send data. In
addition, TCP uses flow control, Congestion Control
and error control mechanisms at the transport level.
Stream delivery
The TCP Connection

 TCP is said to be connection-oriented because


before one application process can begin to send
data to another, the two processes must first
“handshake” with each Other.
 The connection state resides entirely in the two end
systems.
 Because the TCP protocol runs only in the end
systems and not in the intermediate network
elements (routers and link-layer switches), the
intermediate network elements do not maintain TCP
connection state.

4
The TCP Connection

 A TCP connection provides a full-duplex service:


 If there is a TCP connection between Process A on one host
and Process B on another host, then application layer data can
flow from Process A to Process B at the same time as
application layer data flows from Process B to Process A.
 A TCP connection is also always point-to-point, that is,
between a single sender and a single receiver.
 The process that is initiating the connection is called the client
process, while the other process is called the server process.

5
Three way handshake

 The client first sends a special TCP segment; the


server responds with a second special TCP segment;
and finally the client responds again with a third
special segment.

 The first two segments carry no payload, that is, no


application-layer data; the third of these segments
may carry a payload.

Because three segments are sent between the two


hosts, this connection-establishment procedure is
often referred to as a 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

7. The flag field contains 6 bits


8. The ACK bit is used to indicate that the value carried in the
acknowledgment for a segment that has been successfully
received
9. The RST, SYN, and FIN bits are used for connection setup
and teardown
10. Setting the PSH bit indicates that the receiver should pass
the data to the upper layer immediately
11. URG bit is used to indicate that the sending-side upper-layer
entity has marked as “urgent.”

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

 TCP’s sequence numbers reflects over the stream of transmitted


bytes and not over the series of transmitted segments.
 The sequence number for a segment is therefore the byte-
stream number of the first byte in the Segment.
 Example.
 Process in Host A wants to send a stream of data to a process
in Host B over a TCP connection.
 The TCP in Host A will implicitly number each byte in the
data stream.
 Suppose that the data stream consists of a file consisting of
500,000 bytes, that the MSS is 1,000 bytes, and that the first
byte of the data stream is numbered 0.
15
Sequence Numbers and Acknowledgment Numbers

TCP constructs 500 segments out of the data stream.


The first segment gets assigned sequence number 0,
The second segment gets assigned sequence number 1,000,
the third segment gets assigned sequence number 2,000, and
so on.

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

first packet bit arrives


D last packet bit arrives, send
ACK

ACK arrives, send next


packet, t = D + L / R
Pipelining: Motivation
 Stop-and-wait allows the sender to only have a single
unACKed packet at any time
 example: 1 Mbps link (R), end-2-end round trip
propagation delay (D) of 92ms, 1KB packet (L):

Ttransmit = L (packet length in bits) 8kb/pkt


= = 8 ms
R (transmission rate, bps) 10**3 kb/sec

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

 Two generic forms of pipelined protocols: go-Back-N,


selective repeat
Pipelining: increased utilization
sender receiver
first packet bit transmitted, t = 0
last bit transmitted, t = L / R

first packet bit arrives


D last packet bit arrives, send ACK
last bit of 2nd packet arrives, send ACK
last bit of 3rd packet arrives, send ACK
ACK arrives, send next
packet, t = D + L / R

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

 ACK(n): ACKs all pkts up to, including seq # n - “cumulative ACK”


 may receive duplicate ACKs (see receiver)
 timer for each in-flight pkt
 timeout(n): retransmit pkt n and all higher seq # pkts in window
receiver simple:
 ACK-only: always send ACK for correctly-received pkt
with highest in-order seq #
 may generate duplicate ACKs
 need only remember expectedseqnum
 out-of-order pkt:
 discard (don’t buffer) -> no receiver buffering!
 ACK pkt with highest in-order seq #
GBN in
action

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

timeout(n):  in-order: deliver (also


 resend pkt n, restart timer deliver buffered, in-order
pkts), advance window to
ACK(n) next not-yet-received pkt
 mark pkt n as received
 if n smallest unACKed pkt,
advance window base to
next unACKed seq #
Selective repeat in action
Selective repeat:
dilemma
Example:
 seq #’s: 0, 1, 2, 3
 window size=3

 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

Setting the timeout


 RTT plus “safety margin”
 large variation in EstimatedRTT -> larger safety margin

Timeout = EstimatedRTT + 4*Deviation


Deviation = (1-x)*Deviation +
x*abs(SampleRTT-EstimatedRTT)
TCP Connection Management

Recall: TCP sender, receiver Three way handshake:


establish “connection” before
exchanging data segments step 1: client end system sends
 initialize TCP variables:
TCP SYN control segment to
server
 seq. #s
 specifies initial seq #
 buffers, flow control info
step 2: server end system
(e.g. RcvWindow) receives SYN, replies with
 client: connection initiator SYNACK control segment
Socket clientSocket = new
Socket("hostname","port
 ACKs received SYN
number");
 allocates buffers
 specifies server->
 server: contacted by client
receiver initial seq. #
Socket connectionSocket =
welcomeSocket.accept(); step 3: client allocates buffer
and variables, send ACK to
ack SYNACK
TCP Connection Management (cont.)

Closing a connection: client server

client closes socket: close


FIN
clientSocket.close();

Step 1: client end system


sends TCP FIN control close
ACK
segment to server
FIN
Step 2: server receives FIN,

timed wait
ACK
replies with ACK. Closes
connection, sends FIN.

closed
TCP Connection Management (cont.)

Step 3: client receives FIN, client server


replies with ACK. closing
FIN
 Enters “timed wait” -
will respond with ACK
to received FINs
closing
ACK
Step 4: server, receives
FIN
ACK. Connection closed.

timed wait
ACK

closed

closed
TCP Connection Management (cont)
TCP client
lifecycle
TCP server
lifecycle

You might also like