CN 2nd Module
CN 2nd Module
Chapter 3
Transport Layer
02/26/24
Chapter 3 outline
02/26/24
3.1 Introduction and Transport-Layer Services
•A transport-layer protocol provides for logical
communication between application processes running on
different hosts.
Household
Analogy:
12 kids in Ann’s house sending letters to 12 kids in Bill’s house:
• Hosts =
Houses
• Processes =
Kids/cousins
• Application messages =
Letters in envelopes
• Transport-layer protocol =
Ann and Bill who mux/demux to in-house siblings
• Network-layer protocol =
Postal service
02/26/24
Internet transport-layer protocols
application
• reliable, in-order delivery transport
network
data link
(TCP) physical
network
– congestion control network data link
lo
data link physical
gi
physical
ca
– flow control
l
en
– connection setup
d-
en
d
tra
ns
po
• unreliable, unordered
rt
network
delivery: UDP data link
physical
application
transport
network
data link network
physical data link
physical
02/26/24
Chapter 3 outline
02/26/24
3.2 Multiplexing and Demultiplexing
•At the destination host, the transport layer receives segments from
the network layer just below. The transport layer has the
responsibility of delivering the data in these segments to the
appropriate application process running in the host.
•The job of gathering data chunks at the source host from different
sockets, encapsulating each data chunk with header information to
create segments, and passing the segments to the network layer is
called multiplexing.
•At the receiving end, the transport layer examines these fields to
identify the receiving socket and then delivers the segment to
correct socket. This process is called demultiplexing.
02/26/24
Multiplexing/demultiplexing
multiplexing at sender:
handle data from multiple demultiplexing at receiver:
sockets, add transport header use header info to deliver
(later used for demultiplexing) received segments to correct
socket
application
02/26/24
How demultiplexing works
02/26/24
• Each port number is a 16-bit number, ranging from 0 to 65535.
• The port numbers ranging from 0 to 1023 are called well-known port
numbers and are restricted, which means that they are reserved for
use by well-known application protocols.
• such as HTTP (which uses port number 80) and FTP (which uses port
number 21).
• The list of well-known port numbers is given in RFC 1700 and is
updated at http://www.iana.org [RFC 3232].
02/26/24
• Connectionless Multiplexing and Demultiplexing
• Java program running in a host can create a UDP socket with the line
– DatagramSocket mySocket = new DatagramSocket();
• Python program running in a host can create a UDP socket with the
line
– clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
• When a UDP socket is created in this manner, the transport layer
automatically assigns a port number to the socket.
• In particular, the transport layer assigns a port number in the range
1024 to 65535 that is currently not being used by any other UDP port
in the host.
– DatagramSocket mySocket = new DatagramSocket(19157); // Java
– clientSocket.bind((‘’, 19157)) //Python
In this case, the application assigns a specific port number-namely
19157 to the UDP socket
02/26/24
What is the purpose of the source port number?
•As shown in Figure 3.4, in the A-to-B segment the source port number
serves as part of a “return address”—when B wants to send a segment
back to A, the destination port in the B-to-A segment will take its value
from the source port value of the A-to-B segment.
02/26/24
Connection-Oriented Multiplexing and Demultiplexing
•One difference between a TCP socket and a UDP socket is that a TCP
socket is identified by a four-tuple:
•(source IP address, source port number, destination IP address,
destination port number).
•Thus, when a TCP segment arrives from the network to a host, the host
uses all four values to direct (demultiplex) the segment to the appropriate
socket.
02/26/24
• The TCP server application has a “welcoming socket,” that waits for
connection establishment requests from TCP clients (see Figure 2.29)
on port number 12000.
• The TCP client creates a socket and sends a connection establishment
request segment with the lines:
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,12000))
02/26/24
• The transport layer at the server notes the following four values in the
connection-request segment:
(1) the source port number in the segment,
(2) the IP address of the source host,
(3) the destination port number in the segment, and
(4) its own IP address.
02/26/24
• Server B will still be able to correctly demultiplex the two
connections having the same source port number, since the two
connections have different source IP addresses.
02/26/24
Chapter 3 outline
02/26/24
3.3 Connectionless Transport: UDP
•UDP takes messages from the application process, attaches source and
destination port number fields for the multiplexing/de multiplexing
service, adds two other small fields, and passes the resulting segment to
the network layer.
•The network layer encapsulates the transport-layer segment into an IP
datagram and then makes a best-effort attempt to deliver the segment to
the receiving host.
•If the segment arrives at the receiving host, UDP uses the destination
port number to deliver the segment’s data to the correct application
process.
02/26/24
• Why an application developer would ever choose to build an
application over UDP rather than over TCP. Isn’t TCP always
preferable, since TCP provides a reliable data transfer service, while
UDP does not?
• Many applications are better suited for UDP for the following
reasons:
– UDP immediately pass segment, where as TCP not
– No connection establishment
– No connection state. (like- buffers, congestion-control parameters, and sequence
and acknowledgment number parameters)
– Small packet header overhead: The TCP segment has 20 bytes of header
overhead in every segment, whereas UDP has only 8 bytes of overhead.
02/26/24
3.3.1 UDP Segment Structure
02/26/24
• The UDP header has only four fields, each consisting of two bytes
02/26/24
3.3.2 UDP Checksum
•The UDP checksum provides for error detection. That is, the checksum
is used to determine whether bits within the UDP segment have been
altered (for example, by noise in the links or while stored in a router) as
it moved from source to destination.
•UDP at the sender side performs the 1s complement of the sum of all
the 16-bit words in the segment, with any overflow encountered during
the sum being wrapped.
Example, suppose that we have the following three 16-bit words:
–0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 0
–0 1 0 1 0 1 0 1 0 0 1 1 0 1 0 1
–1 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0
02/26/24
• The sum of first two of these 16-bit words is
0110011001100000
0101010101010101
-----------------------------------
1011101110110101
• At the receiver, all four 16-bit words are added, including the
checksum.
• If no errors are introduced into the packet, then clearly the sum
at the receiver will be 1111111111111111.
• If one of the bits is a 0, then we know that errors have been
introduced into the packet.
02/26/24
Chapter 3 outline
02/26/24
3.4 Principles of Reliable Data Transfer
02/26/24
02/26/24
02/26/24
02/26/24
Reliable data transfer: getting started
send receive
side side
• The sending side of the data transfer protocol will be invoked from
above by a call to rdt_send().
• It will pass the data to be delivered to the upper layer at the receiving
side.
02/26/24
3.4.1 Building a Reliable Data Transfer Protocol
02/26/24
• The event causing the transition is shown above the horizontal line
labeling the transition, and the actions taken when the event occurs
are shown below the horizontal line.
• When no action is taken on an event, or no event occurs and an
action is taken, we’ll use Λ the symbol below or above the
horizontal.
• The sending side of rdt simply accepts data from the upper layer via
the rdt_send(data) event, creates a packet containing the data (via the
action make_pkt(data)) and sends the packet into the channel.
• On the receiving side of rdt, receives a packet from the underlying
channel via the rdt_rcv(packet) event, removes the data from the
packet (via the action extract (packet, data)) and passes the data up to
the upper layer (via the action deliver_data(data)).
02/26/24
• Reliable Data Transfer over a Channel with Bit Errors: rdt2.0
02/26/24
02/26/24
02/26/24
02/26/24
02/26/24
Chapter 3 outline
02/26/24
TCP: Overview RFCs: 793,1122,1323, 2018, 2581
02/26/24
02/26/24
• Sequence Numbers and Acknowledgment Numbers
02/26/24
• Host A rcvd all bytes 0 to 535 (0-999) from B
• A is waiting for 536 byte to rcv
• Host A puts ack 536
02/26/24
3.5.3 Round-Trip Time Estimation and Timeout
0.125
02/26/24
Fig: RTT samples and RTT estimates
02/26/24
3.5.4 Reliable Data Transfer
02/26/24
02/26/24
TCP sender events:
data rcvd from app: timeout:
create segment with seq # retransmit segment that
seq # is byte-stream caused timeout
number of first data byte restart timer
in segment ack rcvd:
start timer if not already if ack acknowledges
running previously unacked
think of timer as for segments
oldest unacked update what is known
segment to be ACKed
expiration interval: start timer if there are
TimeOutInterval still unacked segments
02/26/24
TCP: retransmission scenarios
Host A Host B Host A Host B
SendBase=92
Seq=92, 8 bytes of data Seq=92, 8 bytes of data
timeo
ACK=100
X
ut
ut
ACK=100
ACK=120
SendBase=120
X
ut
ACK=120
cumulative ACK
02/26/24
Fig: Retransmission due to a lost acknowledgment
02/26/24
Premature timeout
02/26/24
A cumulative acknowledgment avoids retransmission of the
first segment
02/26/24
Doubling the Timeout Interval
Each time TCP retransmits, it sets the next timeout interval to twice the
previous value, rather than deriving it from the last EstimatedRTT
Fast Retransmit
Retransmitting the missing segment before that segment’s timer expires.
02/26/24
02/26/24
3.5.5 Flow Control
• A speed-matching service.
02/26/24
LastByteRead
LastByteRcvd
02/26/24
• Because TCP is not permitted to overflow the allocated buffer, we
must have
LastByteRcvd – LastByteRead <= RcvBuffer
02/26/24
3.5.6 TCP Connection Management
02/26/24
02/26/24
02/26/24
Life cycle of a TCP connection
• During the life of a TCP connection, the TCP protocol running in
each host makes transitions through various TCP states.
• Figure 3.41 illustrates a typical sequence of TCP states that are
visited by the client TCP.
• The client TCP begins in the CLOSED state.
02/26/24
02/26/24
02/26/24
Principles of TCP Congestion Control
02/26/24
02/26/24
Scenario 2: Two Senders and a Router with Finite Buffers
02/26/24
02/26/24
Network-Assisted Congestion-Control Example:
02/26/24
02/26/24
02/26/24