What is the difference between TCP and UDP?
I know that TCP is used in the case of non-time critical applications, and UDP is used for games or applications that require fast transmission of data. I know that TCP is used for HTTP, HTTPs, FTP, SMTP, and Telnet. I know that UDP is used for DNS and DHCP.
But why? What characteristics of TCP and UDP make it useful for their respective use cases?
Think of TCP as a dedicated scheduled UPS/FedEx pickup/dropoff of packages between two locations, while UDP is the equivalent of throwing a postcard in a mailbox.
UPS/FedEx will do their damndest to make sure that the package you mail off gets there, and get it there on time. With the post card, you're lucky if it arrives at all, and it may arrive out of order or late (how many times have you gotten a postcard from someone AFTER they've gotten home from the vacation?)
TCP is as close to a guaranteed delivery protocol as you can get, while UDP is just "best effort".
TCP
is a connection oriented stream over an IP network. It guarantees that all sent packets will reach the destination in the correct order. This imply the use of acknowledgement packets sent back to the sender, and automatic retransmission, causing additional delays and a general less efficient transmission thanUDP
.UDP
is a connection-less protocol. Communication is datagram oriented. The integrity is guaranteed only on the single datagram. Datagrams reach destination and can arrive out of order or don't arrive at all. It is more efficient thanTCP
because it uses non ACK. It's generally used for real time communication, where a little percentage of packet loss rate is preferable to the overhead of aTCP
connection.In certain situations
UDP
is used because it allows broadcast packet transmission. This is sometimes fundamental in cases likeDHCP
protocol, because the client machine hasn't still received anIP
address (this is theDHCP
negotiaton protocol purpose) and there won't be any way to establish aTCP
stream without theIP
address itself.The Law of Leaky Abstractions by Joel Spolsky
http://www.joelonsoftware.com/articles/LeakyAbstractions.html