! TCP vs UDP ! What's the difference?

I am sure that in your student life, you might have been in a class in which the professor's only concern was to complete the syllabus as quickly as possible without giving a damn whether the students understood the topic or not. On the other hand, there was always a professor who never moved to a new topic until every student in his class had a clear understanding of the topic, no matter how much time it took.

The first class is UDP, second is TCP !!!

UDP(User Datagram Protocol) SOCK_DGRAM

: Connection-less 🤝❌

  1. UDP is known as a connection-less protocol because it is based on request-response architecture. Unlike TCP, it doesn't perform a 3-way handshake to form a connection first and send data.

  2. Once the sender sends a packet, it doesn't expect an acknowledgment from the receiver. Unlike TCP, UDP doesn't maintain a check on the states of the sender and receiver while it is sending the data over the network. UDP is stateless

  3. Technically, the hosts sending data by UDP form a connection but the connection is not persistent.

: Non-reliable 💯❌

  1. When the sender sends the packets, they are not sequenced and the packets that fail to reach the receiver are not re-transmitted by the sender. These properties of UDP make it a very time-efficient data transfer protocol.

  2. But these properties also make the protocol non-reliable, i.e., there is no guarantee that UDP datagrams ever reach their intended destination.

: No flow control🚰❌

To get an understanding of what flow control is, visualize the sender as a tap, flowing water as the datagrams and the receiver as a bucket.

UDP does not provide any kind of flow control over the network, the sender can send datagrams at any speed (even if the receiver is not capable enough to handle incoming data at such high rates) as the receiver is not concerned if the datagram received is not corrupted.

(Consider a bucket that has a hole at the bottom of a certain radius, if the speed of the water flowing from the tap is optimal, then the water coming out of the bucket will have a laminar flow. In the opposite scenario, if the speed of water coming out of the tap is above the optimal limit, then the water will overflow from top of the bucket.)

TCP provides flow control extensively.

: No Sequencing 2️⃣4️⃣1️⃣3️⃣

UDP does not provide any sequencing for the data it sends.

(More on what is sequencing is explained 👇 in the TCP section)

: Use cases ⚒️

  1. Video calling

  2. Online Gaming

  3. Audio Streaming (I guess now you figured out why sometimes you hear a cracked voice while gaming with your friend😉)

TCP(Transmission Control ProtocolSOCK_STREAM

: Not 100% reliable 💯❌

TCP does not give 100% surety that the packet will be delivered to the intended destination, it is impossible. It delivers the packet if possible, and notifies if it is not. The statement given by Richard Stevens, author of Unix Network Programming explains this very well.

" TCP cannot be described as a 100% reliable protocol; it provides reliable delivery of data or reliable notification of failure "

~UNP by Richard Stevens

: Connection🤝✔️

A TCP connection is established only when a 3-way handshake between the client and server is successful. And, every TCP connection is not terminated until one of the parties involved sends a request to do so. Hence, TCP is also known as a connection-oriented protocol.

TCP ensures that no packet is sent till the connection is successful.

TCP by default is full-duplex.

: Acknowledgment👍

TCP connection is based on acknowledgments.

While establishing a connection, the client sends an SYN (synchronize) segment which consists of a sequence number. The server then sends an acknowledgment to the client along with the server's SYN segment. The client then sends an acknowledgment to the server. This is what a 3-way handshake is. This diagram below which I found here, visualizes a 3-way handshake

[ To know in depth this handshake process, go through this article. ]

So, when a TCP client sends data to the other end, it requires an acknowledgment for it in return. If an acknowledgment is not received, then the client retransmits that packet. This doesn't imply that the client will be retransmitting for infinite times, after a few retransmissions, the client will stop retransmitting🔄. This feature makes TCP reliable.

**: RTT(Round trip time)**🔄⏱️

The sender estimates an RTT when it sends a packet to the receiver, if the sender doesn't get an acknowledgment from the receiver about the packet within the RTT, the sender retransmits that particular packet. RTT is measured in milliseconds.

TCP continuously estimates the RTT as it is affected by the network traffic.

The time in the red box represents the RTT of that particular packet, and the time in the orange box is the average RTT of all 4 packets.

**: TTL(Time to live)**⏱️💀

As can be seen in the above figure, every packet has a time to live. If the packet fails to reach the receiver's end within the TTL, it gets destroyed by the router or the processing device that sent it so that it does not circulate in the network endlessly.

UDP lacks this feature.

: Sequence numbers 1️⃣2️⃣3️⃣4️⃣

During the transmission of packets, there might be a few packets that get lost or are not transmitted successfully. TCP retransmits such packets. Now, the problem that arises here is that the receiver has received all the packets, but they are not sequenced(which means that the data sent is corrupted, which is not the desired result). To address this issue what TCP does is that it adds a sequence number in the TCP header with every byte of data that is to be sent.

: Reordering and neglecting🧐🔁🔀

At the receiving end, the receiver reorders the received packets according to their sequence number and then combines them. This solves the issue of data corruption due to network issues.

There might be situations when the sender retransmits a few packets assuming that they are lost, but they arrive a bit late at the receiver's end due to network overload. This creates duplicates and TCP detects them are discards such duplicate packets.

: Use cases ⚒️

  1. Sending Emails

  2. File Transfering

  3. Remote Desktop Accessing

  4. HTTPS requests

I hope you found this article useful and has given you a brief understanding of what these protocols are.

Feel free to drop a comment about any mistakes or any doubts you have.😁🙂