Skip to main content

Command Palette

Search for a command to run...

TCP vs UDP

Updated
4 min readView as Markdown
TCP vs UDP

Introduction

The internet works because it follows rules. Whenever data is sent from one device to another, some protocols decide how that data should travel. There are two main protocols, TCP and UDP one focuses on safety and the other focuses on speed. they make sure our messages, games, and media move across the network.

What is TCP?

TCP (Transmission Control Protocol) is an internet protocol that ensure reliable, sequenced delivery of data between applications/devices.

It is a safe and reliable due to its characteristics that are:

  1. Sequencing

    Since the packets take different routes, they might also arrive out of order. This is solved by TCP using a unique sequence number assigned to each byte sent. then the rebuilds the sequence of data.

  2. Error Checking

    TCP checks if data got corrupted during transfer. if data is wrong it discards the data and sends the resend request.

  3. 3-Way Handshake

    Before sending any data, TCP requires both ends to synchronize, ensuring they are ready for communication (acknowledgement). and this prevents uncoordinated data transmission.

  4. Data integrity

    TCP make sure that the data received is exactly the same as the data sent. no missing bits. No extra bits. TCP achieves this using something called a Checksum.

Working of TCP

  1. Connection Establishment (3-Way Handshake)

    Before sending data, TCP makes sure both sides are ready.

  2. Data Transmission

    Data is sent in small pieces called segments.

    Process:

    • Client sends Data Packet

    • Server sends ACK (Acknowledgement)

    • Client sends next Data Packet

    • Server sends ACK again

This continues until all data is transferred.

  1. Error Detection & Retransmission

    TCP uses checksum to verify data.

  2. Connection Termination (4-Way Handshake)

    it is way of saying both the sender and receiver has done sending and receiving task.

What is UDP?

UDP (User Datagram Protocol) is a transport-layer protocol that provides fast transmission of information via internet connections. It is built for rapid transmission and has a very simple design. unlike TCP, UDP simply sends the data and continues processing without waiting for an acknowledgement from the receiver.

it is a fast but risky due to its characteristics that are:

  • It does not establish a connection before sending data

  • It does not wait for acknowledgments

  • It does not resend lost packets

  • It does not ensure correct order of data

Working of UDP

  • Application Request: Data (such as a voice clip or a gaming instruction) must be sent by an application.

  • UDP Header Added: A short header containing the following is added by the UDP layer when it packs the data into a datagram:

    • Source/Destination Ports: To identify the source and recipient's particular application.

    • Length: Datagram size.

    • Checksum: A field that can be disabled for easy error detection.

  1. IP Handoff: The IP layer receives the datagram and adds its own header (IP addresses) for network routing.

  2. Network Transmission: There is no promise of arrival or order if packets move independently and may take different paths.

  3. Receiver Processing: After receiving packets, the destination host removes IP/UDP headers and uses the port number to send the data to the appropriate application.

  4. No Guarantees: UDP doesn't care if a packet is lost or arrives out of order; the application is responsible for handling it (or not).

Differences

FeatureTCPUDP
ReliabilityHighLow
OrderMaintainedNot guaranteed
ConnectionRequiredNot requied
SpeedSlowerFaster
Error HandlingStrongMinimal

When to Use What

Uses TCP when:

  • Data must be accurate.

  • There is nothing to lose.

Examples include banking, emails, webpages, and APIs

Use UDP when:

  • Accuracy is less important than speed.

Examples include live broadcasts, streaming, video calls, and gaming.

How TCP Relates to HTTP

What is HTTP?

It is an application level protocol used for transmitting data over WWW (world wide web).

the http works on the top of TCP.

TCP delivers HTTP messages safely from client to server.

Without TCP, HTTP would have no reliable transport.

Are TCP and HTTP the same?

They are completely different.

TCP is a protocol that is meant to deliver data safely.

and HTTP is a protocol that is meant for loading webpages using hypertexts links and transmit the data also defines the data exchanges over devices.

Conclusion

TCP and UDP are the backbone of internet communication.

TCP provides us reliability, as well as UDP provides us speed.

and when TCP and HTTP are together, they form a safe web communication.

thank you for reading…hope it helps you understand the core differences and working of both TCP and UDP.