I have had some requests to make a video on the differences between TCP ( Transmission Control Protocol ) and UDP ( User Datagram Protocol).
In this video, I show you the differences between these two protocols and go over some of their use cases. As I mention in the video
Here are the main differences between these 2 most important data transfer protocols.
TCP ( Transmission Control Protocol)
TCP is a protocol used for when you need to ensure that the data from host A reaches host B completely and without errors, an example of this would be FTP ( File Transfer Protocol)
we use FTP to transfer a file from our client machine to a server to save the file. We want to ensure that our file makes it to the server error free to avoid corruption and also we want the entire file
to transfer to the server so we are not missing any data.
TCP requires that both the client and the server maintain a constant connection, this is why TCP is considered a connection-oriented protocol TCP uses a handshake method to establish this connect
see the picture below for the full flow this is also shown in the video.
TCP leverages flags such as ( TCP.syn/TCP.synack/TCP.ack) to identify which type of packet is being sent to and from the client and server.
Using wireshark you can see these flags in action using Wireshark.
You can download Wireshark @ https://www.wireshark.org/.
As you can see from the below Wireshark screenshot we see the 3-way handshake in action.
1). The first packet from 192.168.2.5 to 126.96.36.199 is an SYN packet
2). The response packet from 188.8.131.52 is an SYN-ACK packet
3). The connection is established once 192.168.2.5 responds with an ACK packet
These are the main TCP flags however there are others some of which I mention in the video such as TCP.FIN which signals to end the connection.
Let’s look at the TCP packet header to see how its laid out.
Looking at the below image we can see where the TCP protocol has the mechanisms built into guarantee data delivery.
TCP requires a source port, destination port it also keeps track of the sequence number which is also known as the packet number
that way the other side knows if a packet is missing. Say the client sent 5 packets the server should see packets 1-5 in that order if packets are received out of order that
could signal troubles with the network also if the server only gets 4 packets then that signals a packet has been lost which triggers the client to resend the data.
There is also a field for the control flags that are used for the handshake mechanisim.
some of the other fields such as checksum and window size are dynamically set however you can adjust TCP window size to tweak data transfer rates.
As mentioned in the image a TCP header can range from 20-60 bytes in size this is variable based on the optional data field while the other fields are static sizes that dont change.
TCP uses include HTTP,HTTPS,FTP,SSH amongst others.
UDP ( User Datagram Protocol)
UDP is used for applications in which data does not need to be guaranteed to make it to the other end of the pipe or in a case where we don’t want that to happen.
Take for example a voice call if the packet that is carrying the word hello is somehow dropped we don’t want to have that packet resent as would be the case in TCP.
So that during our conversation the word “Hello” suddenly pops up while we are talking so in this case we use UDP as it does not guarantee that the data makes it from one end to the other.
Also unlike TCP UDP is connectionless meaning that it does not have a handshake mechanism like TCP does it also does not make uses of flags. Like TCP does this makes the packets smaller that TCP packets lets look at the UDP packet header.
Shown below the UDP packet header had 4 fields not counting the data fields the UDP protocol expects the source port , destination port the length of the packet and a calculated checksum. There is also the data being transported i,e a voice conversation unlike TCP where there is a sequence number, an acknowledgment number and so on to count how many packets there are. UDP is much more stripped now with no mechanism to check that a packet has made it to its destination.
There is, of course, an exception to this rule on the application level say an application uses UDP to transfer data and notices some data is missing. The application can send a packet to the client and ask that it send the same data back using UDP. However this is a function of the application layer and not the UDP protocol its self.
Uses of UDP include Voice, DNS,Video amongst others.
I hope this video and post helps you understand the differences between TCP and UDP please let me know if you have any questions !!.