If you are just starting with TCP/IP then you are probably wondering why there are two transport layer protocols- ( TCP and UDP ).
Why don’t we just have one? After all TCP seems to be the best option anyway.
The aim of this short introductory tutorial is to explain the basic differences between the two and why each protocol is needed, and when they are used.
Both TCP and UDP sit at the transport layer of the TCP/IP protocol stack and both use the IP protocol.
Applications are designed to use one or the other protocol depending on their requirements.
TCP- Transmission Control Protocol
TCP is a connection orientated protocol with built in error recovery and re transmission.
You can liken a TCP connection to a telephone connection.
With a telephone connection you first need to setup the connection by dialing the number, and once the calling party answers you have a both way communications channel.
You then proceed to speak and once done you hang up the connection.
With TCP you set up the connection using the 3 way handshake as shown below:
The TCP transport takes care of errors on the link, and the application can be confident that the data received is error free.
TCP is used by applications that need guaranteed message delivery. HTTP,FTP, SMTP, POP3, IMAP4 and many other common Internet application protocols use TCP.
UDP- User Datagram Protocol
UDP is a connectionless protocol.
You can liken UDP to email or the normal post.
With email or a written message you send your message, but have no idea whether or not that message was received.
UDP does not correct or recover errors in the message. Any error detection and recovery is the responsibility of the receiving application.
Because there is no connection setup, UDP is faster than TCP and results in less network traffic.
In addition it doesn’t consume resources on the receiving machine as it doesn’t hold a connection open.
Utility applications like DNS, DHCP, RIP and others use UDP.
The use of UDP is expected to increase with IOT as sensor type data is ideal for sending via UDP.
For more details there is a very useful side by side comparison here
Common Questions and Answers
Q- Can I change what protocol my application uses?
A- No the choice of transport protocol is done by the application developer.
Q- Are TCP ports different than UDP ports?
A- Yes a machine can be communicating on UDP port 2000 and TCP port 2000 at the same time.
TCP and UDP are both transport layer protocols. TCP is a connection orientated protocol and provides reliable message transfer. UDP is a connectionless protocol and does not guarantee message delivery.
Related Articles and resources:
- Understanding the TCP/IP Protocol suite
- Understanding TCP/IP ports and sockets
- Beginners guide to http