Transport layer
The transport layer is critical in providing end-to-end communication services for applications. It
offers mechanisms for error control, flow control, and multiplexing to ensure that data is
transmitted efficiently and reliably across the network. By utilizing port numbers, the transport
layer ensures that data is directed to the correct application process, whether using connection-
oriented TCP or connectionless UDP protocols.
The transport layer is the fourth layer of the OSI (Open Systems Interconnection) model and the
TCP/IP protocol stack. It sits above the network layer and below the application layer, acting as
an intermediary to provide reliable or unreliable communication services between applications
running on different devices within a network. The transport layer ensures that data can be
delivered to the correct application with the necessary guarantees, such as error correction, flow
control, and reliable delivery.
Responsibilities of the Transport Layer
The transport layer is responsible for providing end-to-end communication between devices on
a network. Its primary responsibilities include:
1. Segmentation and Reassembly:
Data generated by the application layer is often too large to be sent in a single packet.
The transport layer breaks the data into smaller units known as segments (in TCP) or
datagrams (in UDP). When the data arrives at the destination, the transport layer
reassembles these segments into the original message.
2. End-to-End Communication:
The transport layer ensures that data reaches the correct destination process running on
the correct device. This is done by using port numbers, which help identify the receiving
application or service.
3. Error Control:
The transport layer provides mechanisms to detect errors that occur during data
transmission. In case of an error (e.g., lost, corrupted, or out-of-order data), it requests
retransmission of the affected data. This is an important function for protocols like TCP.
4. Flow Control:
The transport layer regulates the amount of data sent by the sender to avoid
overwhelming the receiver. It ensures that the sender does not transmit more data than the
receiver can handle, improving the efficiency of the communication.
5. Reliability (for TCP):
Protocols like TCP ensure the reliable delivery of data. This involves mechanisms such
as acknowledgments, sequencing of data packets, retransmissions, and congestion
control. These mechanisms guarantee that data is delivered accurately and in order.
6. Multiplexing and Demultiplexing:
The transport layer allows multiple applications on a device to use the network
simultaneously. This is achieved through port numbers, which allow the data to be sent
to the correct application process on the receiving side. It multiplexes data from multiple
applications into a single stream of data on the sender's side and demultiplexes the
stream at the receiver's side.
7. Connection Control:
The transport layer can either provide a connection-oriented service (TCP) or a
connectionless service (UDP). In the case of a connection-oriented service, a connection
must be established between the sender and receiver before data transmission, whereas,
for connectionless communication, data is sent without establishing a connection.
Addressing in the Transport Layer
Addressing in the transport layer is primarily done through port numbers, which are used to
identify specific processes or applications on a host.
IP Address + Port Number:
A combination of the IP address (network layer) and the port number (transport layer)
uniquely identifies a communication endpoint on a device. This combination is
commonly referred to as a socket.
Port Numbers:
Port numbers allow the transport layer to direct data to the correct application on a
device. There are three main types of port numbers:
1. Well-Known Ports (0-1023): These ports are reserved for standard services and
protocols. Some examples include:
HTTP: Port 80
HTTPS: Port 443
FTP: Port 21
SMTP: Port 25
DNS: Port 53
2. Registered Ports (1024-49151): These are used by software applications and
services that are not universally standardized but still have defined usage.
Examples:
MySQL: Port 3306
Microsoft SQL Server: Port 1433
PostgreSQL: Port 5432
3. Dynamic or Private Ports (49152-65535): These ports are dynamically assigned
by the operating system to client applications when a connection is made. They
are typically used for ephemeral connections and are temporary.
Port numbers help in demultiplexing the data at the receiver side, so the correct application can
process the incoming data.
Protocols on the Transport Layer
There are primarily two transport layer protocols: TCP (Transmission Control Protocol) and
UDP (User Datagram Protocol). Each serves different purposes based on the requirements of the
application.
1. Transmission Control Protocol (TCP)
TCP is a connection-oriented protocol that ensures reliable data transmission between
the sender and receiver. Some key features of TCP are:
o Connection Establishment: Before transmitting data, a reliable connection is
established through a process known as the three-way handshake (SYN, SYN-
ACK, ACK).
o Reliability: TCP guarantees that data will be delivered correctly, in order, and
without duplication. If packets are lost or corrupted, they are retransmitted.
o Sequencing and Acknowledgments: Data is broken into smaller segments, and
each segment is assigned a sequence number. Acknowledgments are sent back to
the sender to confirm receipt of data.
o Flow Control: The sender is regulated by the receiver’s available buffer size to
avoid overwhelming the receiver.
o Congestion Control: TCP adjusts its data transmission rate based on network
congestion to avoid overloading the network.
o Use Cases: TCP is used for applications that require reliability, such as web
browsing (HTTP/HTTPS), file transfer (FTP), email (SMTP), and remote
login (SSH).
2. User Datagram Protocol (UDP)
UDP is a connectionless protocol that provides faster, but less reliable communication
compared to TCP. Some key features of UDP are:
o No Connection Establishment: UDP does not establish a connection before data
transmission. Each packet is sent independently.
o Unreliable: There are no guarantees of delivery, order, or error checking. If
packets are lost or corrupted, they are not retransmitted.
o Low Overhead: UDP has a smaller header size and is more lightweight compared
to TCP. This reduces the protocol overhead.
o No Flow or Congestion Control: UDP does not have mechanisms for flow or
congestion control.
o Use Cases: UDP is ideal for applications where speed is more important than
reliability, such as real-time communication (VoIP), streaming (video, audio),
online gaming, and DNS.
Ports Used by TCP and UDP
Both TCP and UDP use port numbers to distinguish between different applications or services
running on a system. However, they handle connections in different ways:
TCP Ports:
TCP ports are used for connection-oriented services. Each TCP connection is uniquely
identified by the combination of the source IP address, source port, destination IP
address, and destination port.
UDP Ports:
UDP ports are used for connectionless communication. Like TCP, the combination of
source and destination port numbers is used to direct the data to the correct
application. However, UDP does not require the establishment of a connection, making it
faster but less reliable.
Three way Handshaking and TCP Header
The TCP 3-Way Handshake is a fundamental process that establishes a reliable connection
between two devices over a TCP/IP network. It involves three steps: SYN (Synchronize), SYN-
ACK (Synchronize-Acknowledge), and ACK (Acknowledge). During the handshake, the client
and server exchange initial sequence numbers and confirm the connection establishment.
What is the TCP 3-Way Handshake?
The TCP 3-Way Handshake is a fundamental process used in the TCP to establish a reliable
connection between a client and a server before data transmission begins. This handshake
ensures that both parties are synchronized and ready for communication.
Step 1 (SYN): In the first step, the client wants to establish a connection with a server, so it sends
a segment with SYN(Synchronize Sequence Number) which informs the server that the client is
likely to start communication and with what sequence number it starts segments with
Step 2 (SYN + ACK): Server responds to the client request with SYN-ACK signal bits set.
Acknowledgement(ACK) signifies the response of the segment it received and SYN signifies
with what sequence number it is likely to start the segments with
Step 3 (ACK): In the final part client acknowledges the response of the server and they both
establish a reliable connection with which they will start the actual data transfer
TCP Segment Structure
A TCP segment consists of data bytes to be sent and a header that is added to the data by TCP as
shown:
The header of a TCP segment can range from 20-60 bytes. 40 bytes are for options. If there are
no options, a header is 20 bytes else it can be of upmost 60 bytes. Header fields:
Source Port Address: A 16-bit field that holds the port address of the application that is
sending the data segment.
Destination Port Address: A 16-bit field that holds the port address of the application in the
host that is receiving the data segment.
Sequence Number: A 32-bit field that holds the sequence number , i.e, the byte number of
the first byte that is sent in that particular segment. It is used to reassemble the message at the
receiving end of the segments that are received out of order.
Acknowledgement Number: A 32-bit field that holds the acknowledgement number, i.e, the
byte number that the receiver expects to receive next. It is an acknowledgement for the
previous bytes being received successfully.
Header Length (HLEN): This is a 4-bit field that indicates the length of the TCP header by
a number of 4-byte words in the header, i.e if the header is 20 bytes(min length of TCP
header ), then this field will hold 5 (because 5 x 4 = 20) and the maximum length: 60 bytes,
then it’ll hold the value 15(because 15 x 4 = 60). Hence, the value of this field is always
between 5 and 15.
Control flags: These are 6 1-bit control bits that control connection establishment,
connection termination, connection abortion, flow control, mode of transfer etc. Their
function is:
o URG: Urgent pointer is valid
o ACK: Acknowledgement number is valid( used in case of cumulative
acknowledgement)
o PSH: Request for push
o RST: Reset the connection
o SYN: Synchronize sequence numbers
o FIN: Terminate the connection
Window size: This field tells the window size of the sending TCP in bytes.
Checksum: This field holds the checksum for error control . It is mandatory in TCP as
opposed to UDP.
Urgent pointer: This field (valid only if the URG control flag is set) is used to point to data
that is urgently required that needs to reach the receiving process at the earliest. The value of
this field is added to the sequence number to get the byte number of the last urgent byte.
Conclusion
The transport layer is an essential part of the network communication process. It provides a set of
services for end-to-end communication between applications running on different systems. It
ensures data segmentation, flow control, error handling, and reliable or unreliable data delivery,
depending on the protocol used (TCP or UDP). The transport layer also uses port numbers to
direct data to the correct application and provides connection control. By providing multiplexing
and demultiplexing services, the transport layer allows multiple applications to share the same
network connection effectively.
In summary, the transport layer ensures that data is communicated in a manner that meets the
needs of the application, whether that be reliable communication with TCP or fast, lightweight
communication with UDP.