TCP and UDP

TCP and UDP are more important protocols in transport layer.

TCP is used by many services and UDP is used by few services.

TCP provides  reliability and UDP doesn't.

TCP provides error recovery.




TCP requires more bandwidth.

TCP is slow.

UDP needs less bandwidth.

UDP is faster.


Some protocols  which use TCP are mentioned below.

File Transfer Protocol (20,21)
Telnet (23)
Simple Mail Transfer Protocol (25)
Post Office Protocol 3(110)
Hyper Text Transfer Protocol (443)

Some protocols which use UDP are mentioned below.

Dynamic Host Configuration Protocol (67,68)
Trivial File Transfer Protocol (69)
Network Time Protocol (123)
Simple Network Management Protocol (161)

TCP

The below diagram shows the TCP header format.

TCP is a connection-oriented protocol which means that before transmitting the data the sender has to establish a session with the receiver.

Connection establishment and termination in TCP

Connection establishment

Threeway handshake

Sending data over TCP involves the session establishment using a  three-way handshake process.




The host A will send a SYN message to inform the other host that it wants to establish a connection.
After receiving the SYN message from host A , host B replies with SYN-ACK message .
After host A received the SYN-ACK message from host B, it sends an ACK message with ACK number to host B .


Connection termination



Suppose Host A wants to end a connection to host B, Host A will send a FIN message. Host A will be in FIN-WAIT state. In FIN-WAIT state , Host A continues to receive TCP segments from Host B and proceed the segments already in queue, but Host A will not send any additional data.
Device B will confirm it has received the FIN message with an ACK. From this point, Host B will no longer accept data from Host A . Host B can continue sending data to Host A. If Host B does not have any more data to send, it will also terminate the connection by sending a FIN message. Host A will then ACK that segment and terminate the connection.



Windowing


The amount of data that the sender device can send before receiving an acknowledgment from the receiver is called a window.
Windows can be used to control the amount of data segments sent by the sender without receiving an acknowledgement.
The size of the window tells the sender device about how much data segments can be sent before receiving an acknowledgement.





Reliability (Error detection and recovery)


TCP detects the data packet that is damaged or lost during the transmission.

TCP provides an error recovery service to the application layer.




Let's say a PC-A is accessing a webpage from the webserver through web browser.
And the data packet is lost before reaching the web browser. Then the Web page will not be displayed or some content of the page will be missed.

TCP has an error recovery feature.

TCP uses acknowledgments to detect the lost packets and keep track of the packets.

TCP waits for the acknowledgment from the receiver after sending the data packet. If the acknowledgment is not received then TCP will resend the data packet.

Multiplexing


If a computer is using many applications at the same time then there is a chance of data getting mixed up while receiving it.
So TCP and UDP both use Port numbers for each application. The port numbers will be different for different applications.
So while receiving data the data packets are identified using their Port numbers. The port numbers will tell the computer that a particular data packet belongs to a certain application.

By looking at the below two diagrams, you can understand how TCP uses the port numbers.






List of Port numbers below.





UDP


UDP is connectionless.UDP doesn't provide reliability. It won't provide windowing. The applications which are fine with the data loss will only use UDP.

UDP Header format