How a Message Travels Safely Across the Internet: Understanding the Transport Layer
The Transport Layer plays a crucial role in transferring data from the sender’s device to the receiver’s device in a computer network. It is the 4th layer of the OSI Model.
Position of the Transport Layer in the OSI Model
The OSI Model layers above the Transport Layer are:
- Application Layer
- Presentation Layer
- Session Layer
Once data passes through these layers, it reaches the Transport Layer, which prepares the data for transmission across the network.
A Real-Life Example: Sending a WhatsApp Message
Let’s understand the Transport Layer using a simple example.
Suppose you send a message to your friend on WhatsApp.
- You type a message and hit Send.
- The message is handled by the Application Layer (WhatsApp).
- The Presentation Layer encrypts the message.
- The Session Layer manages the session between you and your friend.
- Now, the encrypted message reaches the Transport Layer.
What Exactly Does the Transport Layer Do?
1. Segmentation of Data
The Transport Layer divides the message into smaller units called segments.
This is done because:
- Large data cannot be sent as a single unit
- Smaller segments are easier to manage, transmit, and retransmit if lost
2. Process-to-Process Communication
Unlike lower layers that deal with device-to-device communication, the Transport Layer ensures process-to-process communication.
It does this using port numbers:
- Source Port → identifies the sending application
- Destination Port → identifies the receiving application
Example:
- WhatsApp, browser, and email can all run simultaneously on the same device because they use different port numbers.
3. Handing Data to the Network Layer
Once data is segmented — It is passed one by one to the Network Layer
The Network Layer then:
- Adds IP addresses
- Chooses the best path
- Converts segments into packets
After that, the Data Link Layer and Physical Layer take over for actual transmission.
The Need of the Transport Layer
When you send data from an application, the data moves through the Application, Presentation, and Session layers, but up to this point, the data still belongs only to your application. It has not yet been prepared for actual communication across the network.
This is where the Transport Layer comes into the picture. The Transport Layer takes the data received from the upper layers, divides it into smaller units called segments, and assigns source and destination port numbers to each segment. These port numbers indicate where the data has come from and where it should be delivered, making end-to-end communication possible.
To understand why this is necessary, we first need to understand the concept of a process —
A program in execution is called a process. On your computer, many processes run simultaneously, and several of them may be network-related processes.
For example, when you send a message on WhatsApp, a specific process is created to handle the sending of that message. Similarly, on the receiver’s device, another process is responsible for receiving that message.
Now consider a common scenario: on the receiver’s device, WhatsApp is open, Chrome is being used to browse the web, and YouTube is playing a podcast.
All these applications are receiving data from the network at the same time. From the network’s point of view, all data is arriving at the same machine.
The critical question is: how does the system know which incoming data belongs to which application?
This is exactly the problem the Transport Layer solves.
While the Network Layer provides host-to-host communication using IP addresses, the Transport Layer provides process-to-process communication.
It ensures that data reaches not just the correct device, but the correct application running on that device. The Transport Layer achieves this by using port numbers. Certain applications use fixed or well-known ports, and by attaching destination port numbers to the data, the Transport Layer ensures that the data is delivered to the intended receiving process.
In addition to process identification, the Transport Layer also prepares data for efficient transmission by dividing large messages into smaller segments and adding a Transport Layer header to each segment. This header contains essential information such as port numbers, sequence numbers, and control information. This segmentation plays a major role in ensuring correct, fast, and reliable delivery of data.
At this point, a natural question arises: why is segmentation necessary at all? Wouldn’t dividing data increase complexity or cause loss of information?
The need for segmentation —
Segmentation simply means breaking a large application message into smaller pieces.
- One important reason for doing this is that data is transmitted over physical media such as network cables, which have a maximum transmission size limit. For example, if a network link can transmit only a limited number of bytes at a time, sending a very large message as a single unit would be impossible. By segmenting the data, large messages can be transmitted piece by piece without violating network constraints.
- Another major reason for segmentation is error control and flow control. If a large chunk of data is sent as a single unit and it gets lost or corrupted, the entire data would need to be retransmitted. However, when data is segmented, only the affected segment needs to be resent, which saves bandwidth and time. This makes data transmission far more efficient and reliable.
- Segmentation also ensures fairness among applications. If one application were allowed to send a very large amount of data continuously without segmentation, it could occupy the entire network link, preventing other applications from sending or receiving data. By dividing data into segments, the Transport Layer allows multiple applications to share the network resources fairly.
Hence, segmentation does not cause information loss; instead, it makes data transmission reliable, efficient, and fair, which is one of the core reasons why the Transport Layer is essential in computer networks.
HEADER
Let’s now talk about headers in the Transport Layer.
The Transport Layer uses two different protocols depending on the requirements of communication: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). Each protocol serves a different purpose. TCP focuses on reliability and ordered delivery, while UDP prioritizes speed and low latency. Both protocols have their own advantages and disadvantages, yet both are widely used in everyday networking scenarios such as messaging, browsing, streaming, and online communication.
Since TCP provides reliable data transfer, its header is more complex than that of UDP. Understanding the TCP header is important because it explains how reliability, ordering, and flow control are achieved in practice
Header Size —
The TCP header has a minimum size of 20 bytes and can extend up to 60 bytes when optional fields are included. The minimum size comes from 5 words of 32 bits each, which equals 160 bits or 20 bytes
Let’s now understand each field —
- Source Port (2 byte) — The Source Port field is 2 bytes long and identifies the port number of the sending process. It tells the receiver which application on the sender’s machine initiated the communication.
- Destination Port (2 byte) — Also 2 bytes long, specifies the port number of the receiving process. This is how TCP ensures that incoming data is delivered to the correct application.
- Sequence Number (4 byte)— It plays a crucial role in reliable data transfer. When an application message is divided into segments, TCP assigns a sequence number to each segment. This number represents the position of the first byte of data in that segment relative to the entire data stream. At the receiver’s side, sequence numbers help in reordering segments that may arrive out of order and in detecting missing data.
- Acknowledgment Number (4 byte)— This field confirms successful reception of data. Instead of acknowledging each segment individually, TCP uses cumulative acknowledgment. The acknowledgment number indicates the next byte the receiver expects, implicitly confirming that all previous bytes have been received correctly.
- Header Length (4 bits) —Also known as the Data Offset, it specifies the length of the TCP header in terms of 32-bit words. This is necessary because the header size is not fixed; it may increase when options are included. By knowing the header length, the receiver can correctly determine where the actual data begins.
- Window Size — It indicates how much data the receiver is willing to accept at a given time. This prevents the sender from overwhelming the receiver by sending data faster than it can be processed.
- CheckSum — TCP calculates a checksum over the header and data before transmission. When the segment reaches the receiver, the checksum is recalculated. If the values do not match, the segment is considered corrupted and is discarded, triggering retransmission.
- Options —It allows TCP to support additional features such as maximum segment size (MSS), window scaling, and timestamps. This field is optional and is the reason why the TCP header can grow beyond its minimum size.
Wrap-Around Time
One important concept related to the TCP header is wrap-around time, which is associated with sequence numbers.
Get Ayushi Pal’s stories in your inbox
Join Medium for free to get updates from this writer.
The sequence number field in TCP is 32 bits long, meaning it can represent values from 0 to 2³²-1. Since TCP treats data as a continuous stream of bytes, sequence numbers increase as data is transmitted. If a connection remains active long enough and enough data is sent, the sequence numbers will eventually reach their maximum value and wrap around back to zero.
Wrap-around time refers to the time it takes for the sequence numbers to cycle completely. This concept is important because TCP must ensure that old segments are not confused with new ones after wrap-around occurs. TCP avoids this confusion by using mechanisms such as connection timeouts, acknowledgment tracking, and careful sequence number management.
WAP = 2³² bytes / Transmission rate
UDP Header
The UDP (User Datagram Protocol) header is much simpler than the TCP header. It is a fixed-size header of 8 bytes, which makes UDP lightweight and fast. Because of this minimal overhead, UDP is commonly used in applications where speed is more important than reliability.
The UDP header consists of only four fields. The Source Port identifies the port number of the sending process, while the Destination Port specifies the port number of the receiving process. These port numbers allow UDP to support process-to-process communication, similar to TCP, but without additional reliability mechanisms.
The Length field indicates the total length of the UDP segment, including both the header and the data. Since this field is 16 bits long, the maximum size of a UDP segment is limited (2¹⁶-1). The Checksum field is used for error detection. It helps verify whether the data was corrupted during transmission. In IPv4, the checksum field is optional, whereas in IPv6 it is mandatory.
Once the UDP header is added, each segment is passed to the Network Layer, which further encapsulates it inside an IP packet. At the receiver’s side, when the packet reaches the Transport Layer, the UDP header is examined, the port numbers are checked, and the data is delivered to the appropriate application. The Transport Layer removes its own header before handing the data to the upper layers.
Other layers do not modify the Transport Layer header; they only add and remove their own headers during encapsulation and decapsulation.
What Are TCP and UDP?
To understand when UDP is used, it is important to clearly distinguish it from TCP.
TCP (Transmission Control Protocol) is a connection-oriented protocol. Before any data is transmitted, a connection is established between the sender and receiver using the three-way handshake mechanism. Only after this connection setup is complete does data transmission begin. This is similar to making a phone call — first the call is connected, and only then can communication take place. TCP provides reliability through error control, flow control, congestion control, and ordered delivery of data.
UDP, on the other hand, is a connectionless protocol. There is no prior connection setup between the sender and receiver. Data is sent immediately, which makes UDP significantly faster than TCP. Because of this, UDP is well suited for applications such as video calls, live streaming, online gaming, and real-time data transmission, where occasional data loss is acceptable but delays are not.
However, this speed comes at a cost. UDP does not provide flow control, congestion control, or retransmission mechanisms. As a result, it is considered unreliable compared to TCP. If a UDP segment is lost or corrupted, it is not retransmitted by the protocol itself.
UDP Data Size Limitation and Use Case
The maximum size of a UDP segment is 65,507 bytes ( 2¹⁶-1-header length) for application data. This limitation arises due to the size of the IP and UDP headers within the maximum IP packet size. Therefore, UDP is generally used for small-sized data transfers, especially in real-time scenarios where quick delivery is more important than guaranteed delivery.
In summary, UDP is best suited for fast, real-time communication involving small chunks of data, while TCP is preferred for applications that require accuracy, reliability, and ordered delivery.
TCP Handshake
As I discussed earlier, TCP is a connection-oriented protocol, which means that before any actual data transfer takes place, a connection must first be established between the sender and the receiver.
TCP achieves this connection setup through a three-step process known as the TCP three-way handshake.
- The client starts the process and sets the SYN ( Synchronisation ) flag to 1
- The client sends the message to the server by setting the SYN flag to 1.
- The message contains some other information like sequence numbers(2000), maximum segment size, window size.
- Sequence Number will be randomly generated.
- SYN — It stands for synchronization. It sends the message to the server to synchronize with the received sequence number.
2. The server replies with (SYN+ACK)
- After receiving the synchronization request from the client, the server sends the acknowledgment to the client by setting the ACK flag to 1.
- It sends the Acknowledgment number (ACK) to the client which is just 1 greater than the received sequence number from the client.
- In addition to this, the server also sends a randomly generated sequence number and sets the SYN flag to 1.
- The server also sends its window size which is the buffer capacity of the server and maximum segment size to the client for flow control.
3. The final acknowledgment from the client (ACK)
- After receiving the SYN from the server, the client sets the ACK flag to 1.
- It sends the expected sequence number by the server which is equal to the Acknowledgment number received from the server.
- It also sends the Acknowledgment number which is one greater than the sequence number received from the server which denotes the next expected sequence number.
If either side does not receive the expected acknowledgment within a specified time limit, the connection setup fails and the handshake process is retried.
TCP Congestion Control
Unlike UDP, TCP includes built-in congestion control mechanisms to prevent excessive data from overwhelming the network. Congestion occurs when the path through which data travels becomes overloaded. Routers along the path have a limited buffer capacity, and when multiple sources send large amounts of data simultaneously, these buffers can overflow, leading to packet loss.
To handle this, TCP controls the amount of data it sends using a windowing mechanism. The actual sending window size is determined by the minimum of the receiver’s window size (rwnd) and the congestion window size (cwnd):
Sending Window=min(rwnd,cwnd)
Notation —
Sender’s window size = swnd
Receiver’s window size = rwnd
Congestion window size = cwnd
rwnd is already known during TCP Handshake process. But since cwnd depends on the traffic of data, it is not fixed.
TCP handles it through 3 steps —
- Slow Start
- Congestion Avoidance
- Congestion Detection
Slow Start —
The TCP starts with cwnd as 1. This is the min. we can take. Once, we receives ACK, it guarantees that atleast this much window size is available. After that, we always doubles the window size.
As a result, the window grows exponentially (1, 2, 4, 8, 16, and so on). This rapid increase is known as exponential increase of cwnd and it continues until the congestion window reaches a predefined limit known as the slow start threshold (ssthresh).
Congestion Avoidance —
Once the threshold is reached, TCP enters the congestion avoidance phase. In this phase, TCP increases the congestion window much more carefully. Instead of doubling, the window size grows linearly, usually by one segment per round-trip time. This cautious approach reduces the risk of triggering congestion again.
Congestion detection —
There are two ways of detection —
- Time-out-timer
- 3 duplicate ACKS.
Time-Out-Timer — If an ACK is not recevied with the time out timer expiration limit, this indicates that the path is very much congested. Here for recovery, we make the cwnd to 1 and starts the process from slow start.
3-Duplicate ACKS — If we receives ACK’s for same sequence number, that means that sequence got lost due to congestion. But since we recevied ACK that means congestion is very mild. So, here we just set ssthresh to half and cwnd becomes ssthresh and again we start linear increase. This is know as fast recovery.
A common doubt is how multiple duplicate acknowledgments are generated.
As from above diagram you can see, segment 1 will send ACK = segment 2. But segment 2 got lost. Now, segment 3,4,5 both sent ACK = segment 2 because it is not yet received. Hence, in this way 3 duplicate ACK are recevied.
I hope you knw that the receiver continues to acknowledge the last correctly received segment while receiving subsequent out-of-order segments. As a result, the sender receives multiple acknowledgments carrying the same acknowledgment number.
That’s it for Transport Layer! You now have complete understanding of TCP!
Let’s meet again in next topic — Network Layer




