TCP Vs UDP: What is the Difference Between TCP and UDP?

In this article, we will learn What is the Difference Between TCP and UDP? first, we have to Understand What is TCP ?, What is UDP? then we can Understand TCP VS UDP & What is the Difference Between TCP and UDP?

What is TCP?

TCP Stands For Transmission Control Protocol. It is the most commonly used Protocol, it is full Duplex, Connection-oriented, reliable and Accurate protocol.

In TCP, it takes large block of data (Information) and breaks into them it gives the sequence number to each Segment So Destination will Stack data to get in original Message, After sending the segment it will wait for the acknowledgement from the receiver side, if it will not receive the acknowledgement it will resend that  segment again, by using acknowledgement it will do error correction

TCP uses Connection-oriented Communication, it will Create a virtual Circuit before Transmitting the segment to the destination.

AS  TCP is Connection-oriented Protocol, a Connection is a need to be established before two devices start Communication, TCP uses a process Known as three-way Communication, During this initial handshake, the two TCP Layers agree on the Amount of Information that going to be sent before the Receiptants TCP sends back an acknowledge.

TCP 3-way Handshaking Process

As TCP is Connection Oriented Protocol, it uses a process known as three-way Handshaking, before establishing the Connection with the Destination.

TCP 3-Way Handshaking
TCP 3-Way Handshaking

STEP1 (SYN): Clients Wants to establish the Connection with Destination host/Server.it will send a Segment with SYN Which  inform the destination host that source wants is like to start communication with Destination server and what is sequence number  from which it will start the Segment

STEP2 (SYN +ACK): In responding to the request of Source client, Destination will Send SYN + ACK  to the Source, in Which Acknowledgement (ACK) signifies the response of the Segment received and  SYN Signifies with which Sequence number it is likely to start the segment with

STEP 3 (ACK): In this Step Client acknowledges the Server response and both establish a reliable Connection with which they will Start an actual data communication

TCP Connection Termination

After Data transmission Process is finished TCP will Terminate the Connection between two endpoints. This is the Four-Step Process

TCP Connection Termination
TCP Connection Termination

1.) The Client application that wants to close the connection will send a TCP segment with the FIN (finished ) Flag is set 1.

2). The Server receives the TCP segment and acknowledges back it with the acknowledge (ACK) segment

3). The server sends its own TCP segment with FIN with the Flag to set to 1, to the client in order to  close the Connection

4). The Client acknowledges (ACK) the Server FIN Segment Closes the Connection.

TCP Header

TCP Header
TCP Header

Source Port: This is the port number used by the host application that wants to send the data. Source Port is a 16-bit field.

Destination Port: the port number of the destination host to which source wants to communicate. Destination Port is a 16-bit field.

Sequence Number: A number used by TCP that helps to reorder the segment incorrect order at receiving end.

Acknowledgement number: It contains the sequence number of the data byte that the receiver expects to receive next from the sender. It is always sequence number of the last received data byte incremented by 1

Header Length: Header length is a 4-bit field. It contains the length of the TCP header. It helps in knowing where the actual data begins.

Reserved: Always set to zero.

Code bits/Flags: it controls Functions used to set up and terminate a session

Window: the Window size the sender is willing to accept, it is Window size is a 16-bit field, it advertises how much data (in bytes) the sender can receive without acknowledgement

Checksum: it verifies the integrity of data in the TCP payload. The Sender adds CRC checksum to the checksum field before sending the data. Receiver rejects the data that fails the CRC check.

Urgent: Urgent pointer is a 16-bit field. It indicates how much data in the current segment counting from the first data byte is urgent. Urgent pointer added to the sequence number indicates the end of urgent data byte. This field is considered valid and evaluated only if the URG bit is set to 1.

Option: The urgent pointer field is often set to zero or ignored, but in conjunction, it is one of the control flags, it can be used as a data offset to mark a subset of a message as requiring priority processing.

Data: Handed down to the TCP protocol at the transport layer, which includes the upper-layer header.

What is UDP?

UDP Stands For User Datagram Protocol, UDP is transport layer Protocol, It is Connectionless unreliable Protocol.

UDP does not Sequence the segment, It doesn’t ‘t care about the order of the segment received at the destination, it just sends the segment and forgets about it.

It does not check for the acknowledgement, that is why it is unreliable Protocol.

It is connectionless protocol, It doesn’t create a virtual circuit with the destination, or does not contact with destination before delivering the Segment.

It just sends the segment to segment. If the destination receives the data in out of order, it will simply pass the segment to the next application layer, It does not do any error checking.

UDP Header

UDP Header
UDP Header

Source Port: Port No of source that sending data

Destination Port: Port No of destination that receiving data.

Checksum: it verifies the integrity of data in the UDP payload. the Sender adds CRC checksum to the checksum field before sending the data. Receiver rejects the data that fails the CRC check.

Data: Upper layer data.

TCP VS UDP: Difference between TCP and UDP

                         TCP                             UDP
Sequence Consequence
Reliable Unreliable
Connection-Oriented Connectionless
It creates a Virtual Circuit It Does not Create a Virtual Circuit
It uses acknowledgement No Acknowledgement
Speed –Faster Speed –Slower as compare to TCP
Error Detection No Error Detection
Header Size: the packet is heavy due to overhead. Header Size: a lightweight packet with minimal header

Sequencing: TCP adds sequence number to each segment, it helps the recipient to rearrange the segment in receiving end, whereas UDP does not add Any sequence number it just send the segment to the destination

Connection: TCP is Connection-Oriented it Creates the Virtual Circuit with destination by using a process known as three-way handshaking. Whereas UDP is Connection-less  each segment is sent to destination without reliable Communication

Speed: UDP Does not have many requirements, it offers a faster connection due Header Size is less than TCP,If you need more speed less reliability you can use UDP.

Reliability: Due to Connection-oriented communication, sequencing, acknowledgement TCP provides reliability whereas UDP Does not use these things.

Header Size: Since TCP provides more functionality such as sequence, error detection,acknowledgement.the header of TCP packet is larger Than UDP packet .this makes each packet heavy .this is why TCP Connection is Slower than UDP  Connection.

Acknowledgement: When TCP packet is received at destination end it will send the acknowledgement to the sender, if the sender does not receive any acknowledgement, it will assume that the packet was delivered is corrupted and it will resend that segment to the Destination.

On the other hands, In UDP does not use acknowledgement .so the sender will not know if the segment were not received at the receiver end.

TCP Ports VS UDP Ports

There can be 65535 TCP ports and 65535 UDP Ports.it depends on the protocol on which port number it is using.

TCP Well KnownUDP Known Port
FTP  -Port 20,21DHCP -Port 67,68
SSh Port 22SNMP -161,162
SMTP -25 ,POP3 -110Telnet 21
HTTP -80 ,HTTPS -44388 -XBOX Gaming service

Why UDP is Faster than TCP?

TCP uses more functionality such as sequence, error detection, error Correction, acknowledgement Due to Size the header of TCP packet is larger than UDP packet .this makes each packet heavy whereas UDP Does not use such Functionality due to which size header is smaller than TCP, that why UDP is Faster than the TCP.