What is TCP?
TCP (Transmission Control Protocol) is a set of rules that makes sure data sent from one computer reaches another computer accurately and in the correct order.
Features of TCP
- Connection-Oriented: TCP establishes a connection between the two devices before sending any data. It’s like making sure someone is home before delivering a letter.
- Reliable: TCP checks to make sure every piece of data (called a packet) arrives at its destination correctly. If any packet is missing or damaged, it will retramit the data.
- Orderly Data Transmission: TCP ensures that packets arrive in the correct order, just like how you’d want the pages of a book to be in the right sequence.
- Flow Control: TCP controls the amount of data being sent to prevent the receiving device from being overwhelmed, similar to how a teacher might pace a lesson to ensure students keep up.
Applications of TCP
TCP is used in situations where accuracy and reliability are crucial. Some common applications include:
- Web Browsing: When you visit a website, TCP ensures that all the text, images, and videos load correctly.
- File Transfer: When you download or upload files, TCP makes sure the entire file is transferred without errors.
- Email: TCP ensures that your emails are delivered completely and in the right order.
How TCP Works
- Connection Establishment: Before sending data, TCP sets up a connection between the sender and receiver using a process called the "three-way handshake."
- Data Transmission: TCP breaks the data into small packets and sends them one by one. Each packet is numbered so that they can be reassembled in the correct order.
- Acknowledgment: The receiver sends an acknowledgment (ACK) back to the sender to confirm that each packet was received.
- Error Checking: If any packet is lost or damaged, TCP detects it and resends the affected packet.
- Connection Termination: Once all the data has been sent and acknowledged, TCP closes the connection.
3-Way handshake of TCP
The 3-way handshake is a fundamental process in the Transmission Control Protocol (TCP) used to establish a reliable connection between a client and a server over a network. This handshake ensures that both parties are ready to communicate and that the connection is properly synchronized. Here’s how it works:
1. SYN (Synchronize) - Client to Server. The process begins when the client wants to establish a connection with the server. The client sends a TCP packet with the SYN (synchronize) flag set. This packet contains an initial sequence number (ISN), which is a randomly chosen value. This ISN will be used as a reference for the data that will be transmitted in the session.
2. SYN-ACK (Synchronize-Acknowledge) - Server to Client. Upon receiving the SYN packet, the server acknowledges the client’s request by sending back a packet with both the SYN and ACK (acknowledge) flags set. The SYN flag is set to indicate that the server is willing to establish a connection, and the ACK flag is set to acknowledge the client’s SYN packet. The server also includes its own ISN in this packet.
3. ACK (Acknowledge) - Client to Server. Finally, the client sends an ACK packet back to the server. This packet acknowledges the receipt of the server’s SYN-ACK packet and includes the next expected sequence number for the server’s data. At this point, the connection is established, and both parties can start exchanging data.
Summary of the Process:
Step 1: Client sends SYN (Synchronize) to Server.
Step 2: Server responds with SYN-ACK (Synchronize-Acknowledge).
Step 3: Client sends ACK (Acknowledge) back to Server.
After these three steps, a reliable connection is established, and data transfer can begin between the client and server.
What is UDP?
UDP (User Datagram Protocol) is another set of rules for sending data over the internet, but it’s much faster and simpler than TCP. However, it doesn’t guarantee that all the data will arrive correctly or in order.
Features of UDP
- Connectionless: UDP doesn’t establish a connection before sending data. It’s like sending a letter without checking if someone is home to receive it.
- Faster: Since UDP doesn’t worry about checking for errors or making sure all packets arrive, it can send data much faster than TCP.
- No Reliability: UDP doesn’t guarantee that the data will arrive correctly or even at all. It’s like sending postcards without worrying if one gets lost.
- No Order: UDP doesn’t gu that the packets arrive in the correct order.
Applications of UDP
UDP is used when speed is more important than reliability. Common applications include:
- Live Streaming: When you’re watching a live video, UDP is used because it’s more important to receive data quickly than to receive every bit of it perfectly.
- Online Gaming: In fast-paced online games, UDP is used to send game data quickly, even if some packets are lost along the way.
- Voice and Video Calls: For real-time communication, like voice and video calls, UDP is preferred because it minimizes delays.
How UDP Works
- No Connection Establishment: UDP doesn’t establish a connection before sending data. It just starts sending packets.
- Data Transmission: The data is divided into packets and sent to the receiver without waiting for any acknowledgment.
- No Error Checking: UDP doesn’t check if all the packets arrived or if they’re in the correct order. It just sends the data as fast as possible.
- No Connection Termination: Since there’s no connection to start with, there’s nothing to close after sending the data.
Let’s compare TCP and UDP side by side:
Feature |
|
|
||||||
Connection |
|
|
||||||
Reliability |
|
|
||||||
Speed |
|
|
||||||
|
|
|
||||||
|
|
|
||||||
|
|
|
||||||
|
|
Smaller overhead, faster transmission |