In our earlier article about how data travels across networks, we learned about the Transport Layer and how it handles communication between applications running on different devices.
We also briefly discussed two protocols: TCP and UDP.
If you haven't read that article, you can check it out first to understand what the Transport Layer does.
Now, let's understand how TCP and UDP actually work, how they're different, and when we use each of them.
Let's start with UDP.
UDP (User Datagram Protocol)
Let's say you're on a video call with your friend.
While you're talking, some of the audio data gets lost because of a network issue.
You might notice that the audio breaks for a second, but the conversation continues.
Now, imagine if your phone stopped the call and waited for that missing audio to arrive.
By the time it arrives, your friend might have already said something else.
So, in situations like this, receiving the latest data quickly can be more useful than waiting for missing data.
UDP is useful for this kind of communication.
How Does UDP Work?
Suppose your application wants to send some data to your friend's application.
With UDP, it can send the data without first establishing a connection with the receiver.
That's why UDP is called a connectionless protocol.
It also doesn't wait for the receiver to confirm that the data was received.
But there are a few things we should know about UDP.
1. Delivery is not guaranteed
Suppose we send three messages:
Message 1 → Message 2 → Message 3
If Message 2 gets lost somewhere on the network, UDP doesn't automatically send it again.
The receiver might get only Message 1 and Message 3.
2. Order is not guaranteed
Even if we send messages in this order:
1 → 2 → 3
They might arrive as:
2 → 1 → 3
UDP doesn't arrange them back into the original order.
3. UDP doesn't check whether the receiver is ready
UDP doesn't provide flow control.
If the sender sends data faster than the receiver can handle, some data may be dropped.
4. UDP can detect corrupted data
Suppose you're sending some data to your friend, but a few bits change while the data travels through the network.
How would your friend's device know that something changed?
UDP uses something called a checksum.
Before sending the data, the sender calculates a checksum based on the data and some other information.
The checksum is sent along with the data.
When the receiver gets it, it checks whether the checksum is valid.
If the check fails, the receiver can tell that the data was corrupted, so it normally discards it.
But UDP doesn't automatically send that data again.
What Does a UDP Datagram Look Like?
When your application sends a message, UDP adds a small header to it.
This header contains four fields:
- Source Port: The port from which the data is sent.
- Destination Port: The port to which the data should be delivered.
- Length: The total size of the header and data.
- Checksum: Used to check whether the data was corrupted.
The header takes up just 8 bytes.
The header and the actual data together are called a UDP datagram.
Now suppose your application sends three separate messages.
Message 1 → Message 2 → Message 3
UDP adds a header to each message, creating three separate datagrams.
Unlike TCP, UDP doesn't automatically divide a large application message into smaller datagrams.
Where Is UDP Used?
UDP is commonly used in:
- Video and voice calls: Where receiving the latest audio or video is more useful than waiting for missing data.
- Online gaming: To send frequent updates, such as player positions and movements.
Now, let's see how TCP works differently.
TCP (Transmission Control Protocol)
We just saw how UDP sends data without establishing a connection with the receiver.
TCP works differently.
Suppose you're downloading a file from a server.
Before the server starts sending data over TCP, a connection is established between your laptop and the server.
That's why TCP is called a connection-oriented protocol.
Once the connection is established, the server can start sending the file.
But it doesn't send the entire file at once.
TCP takes the stream of data and sends it in smaller pieces called segments.
Each segment contains a TCP header along with some of the actual data.
Now, how does TCP establish that connection?
How Does TCP Establish a Connection? (Three-Way Handshake)
Suppose you open a website in your browser.
Before your browser and the web server exchange data over TCP, they establish a connection using a process called the Three-Way Handshake.
Let's see how it works.
1. SYN — Client to Server
The client sends a SYN message to the server, asking to establish a connection.
It also sends an initial sequence number.
For example:
SYN, Sequence Number = 100
2. SYN-ACK — Server to Client
The server responds with a SYN-ACK message.
It acknowledges the client's SYN and sends its own initial sequence number.
For example:
SYN-ACK, Sequence Number = 500, Acknowledgment Number = 101
The acknowledgment number is 101 because the client's SYN used sequence number 100.
3. ACK — Client to Server
The client sends a final ACK to acknowledge the server's SYN.
ACK, Sequence Number = 101, Acknowledgment Number = 501
Here, 501 acknowledges the server's SYN, which used sequence number 500.
Now the TCP connection is established, and both sides can start exchanging data.
How Does TCP Handle Missing Data?
Now suppose you're downloading a file from a server.
The server sends the data in smaller segments. But what if some of those segments get lost or arrive in the wrong order?
TCP uses sequence numbers and acknowledgments (ACKs) to keep track of the data.
A sequence number tells us where the data belongs, while an ACK tells the sender which byte the receiver expects next.
Let's see what happens in different situations.
1. What If Some Data Goes Missing?
Suppose the server sends three segments, but the second one gets lost.
Your laptop receives the first and third segments, but it's still waiting for the second one.
It sends an acknowledgment (ACK) showing which data it expects next.
The server can use these acknowledgments to detect missing data and send it again. TCP can also resend data if its timer expires.
Once the missing data arrives, your laptop can pass all three segments' data to the application in the correct order.
2. How Does TCP Send Missing Data Again?
Suppose the server sends some data but doesn't receive an ACK.
How long should it wait?
TCP uses a timer to decide when to send unacknowledged data again.
If the timer expires before the data is acknowledged, TCP can send that data again.
This is called retransmission.
TCP can also detect missing data through acknowledgments and retransmit it without waiting for the timer to expire.
3. What If the ACK Gets Lost?
Now suppose your laptop receives the data and sends an ACK, but the ACK gets lost.
The server doesn't know that the data has already arrived, so it might send it again.
Your laptop can identify the repeated data using its sequence numbers and avoid passing the same bytes to the application twice.
4. What If Data Arrives in the Wrong Order?
Suppose the server sends three segments in this order:
1 → 2 → 3
But your laptop receives them as:
1 → 3 → 2
TCP uses sequence numbers to put the data in the correct order before passing it to the application.
So, TCP can handle missing, repeated, and out-of-order data using sequence numbers, ACKs, and retransmissions.
But what if the server sends data faster than your laptop can receive it?
Let's see how TCP handles that using flow control.
How Does TCP Handle Flow Control?
Suppose you're downloading a large file from a server.
The server is sending data quickly, but your laptop is taking some time to process it.
If the server keeps sending more data, your laptop's receiving buffer might become full.
So how does the server know how much data your laptop can handle?
TCP uses something called flow control.
Your laptop tells the server how much free space is available in its receiving buffer.
This is called the receive window.
For example, if your laptop has space for another 5,000 bytes, it can tell the server that through the receive window.
As the application reads the received data, more space becomes available, and the laptop can update the server.
If the receive window becomes zero, the server normally stops sending new data until space becomes available.
This way, TCP avoids sending more data than the receiver can handle.
But what if your laptop is ready to receive data, while the network itself is busy?
How Does TCP Handle Congestion Control?
Suppose you're downloading a file, and many other people are using the same network.
If everyone keeps sending too much data, routers may become overloaded and start dropping packets.
TCP uses congestion control to avoid making the problem worse.
When TCP starts sending data, it usually begins with a limited amount and gradually increases how much it sends.
If it notices signs of congestion, such as packet loss, it can reduce how much data it sends.
As the network conditions improve, it can increase the amount again.
So, the difference is simple:
- Flow control: Makes sure the receiver isn't getting more data than it can handle.
- Congestion control: Tries to avoid sending too much data into a busy network.
Next, let's see how TCP allows both sides to send and receive data using the same connection.
How Does TCP Handle Full-Duplex Communication?
Suppose you open a website in your browser.
Your browser sends a request to the server, and the server sends data back.
But what if your browser needs to send some more data while the server is still sending its response?
TCP allows both sides to send and receive data at the same time using a single connection.
This is called full-duplex communication.
Both sides have their own sequence numbers and acknowledgments to keep track of the data they send and receive.
So, we don't need two separate TCP connections just to send data in both directions.
Conclusion
So, we've seen how TCP and UDP handle communication differently.
TCP is useful when we need reliable delivery and data in the correct order.
UDP is useful when we want to send data without waiting for acknowledgments, especially in real-time applications.
Neither protocol is better in every situation. It depends on what the application needs.
That's all for this article!
Thanks for reading. See you in the next part of the Networking Essentials series.









Top comments (0)