What is the difference between UDP and TCP?
Solution 1
TCP is backed by acks and retries to make sure you data gets where it's going. UDP is connectionless and "fire and forget". UDP is mostly used for streaming type applications, where if you lose some data you don't need to try to send it again.
Which one you use depends on the application. For example, a web server uses TCP.
Solution 2
You can find a good summary here:
What is the difference between UDP and TCP internet protocols?
Both TCP and UDP work at transport layer TCP/IP model, but have very different usage.
The most important differences are:
-
Reliability:
TCP: connection-oriented
UDP: connectionless -
Ordered:
TCP: order of message receipt is guaranteed
UDP: order is not guaranteed -
Protocol weight:
TCP: heavyweight, because of the connection/ordering overhead
UDP: lightweight, very few overhead -
Packets:
TCP: streaming, data is read as a "stream," with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call.
UDP: datagrams, one packet per one read call.
Frame structure
When data is sent over the network, it needs to be encapsulated into so called "frames." There are various methods of encapsulation depending on the protocol and topology that are being used. The following images show how TCP and UDP frame structures differ.
This is the TCP frame structure:
An this the UDP frame structure, much simpler:
Typical protocols which use TCP are HTTP, FTP and SMTP. Examples of protocols using UDP are DNS and DHCP.
Solution 3
And the CEO level explanation:
UDP is when you throw your paper in the general direction of the bin.
TCP is when it misses, you throw exact copies of the same paper again and again until it falls into the bin. There would be paper wastage, even resent TCP packets result in wastage of network or system resources.
Solution 4
TCP and UDP are both protocols that run on top of IP. TCP has guaranteed delivery and UDP does not. You would select one or the other for port forwarding depending on what service you're trying to forward. HTTP, for instance is TCP. If you don't know what protocol the service you're trying to forward is, it's almost certainly TCP.
Solution 5
To answer other part of your question, you have to forward what your application uses. To forward HTTP traffic, select TCP. To forward TFTP traffic, select UDP. p2p programs mostly use both tcp and udp, so forward them both.
It all depends on the protocol and the program you are using.
Related videos on Youtube
Guy
I am a consultant specializing in TypeScript/React/JavaScript/Node.js Full Stack. I also have a strong background in C#/.Net. I am available for hire.
Updated on September 17, 2022Comments
-
Guy over 1 year
My router has two protocols (and a "both" option) that I can select when setting up port forwarding: UDP and TCP. What is the difference between these two protocols and when would you select one over the other in port forwarding?
-
Guy almost 15 yearsThat makes sense - so if you're streaming a video there's not point in resending a missing frame later because you've already passed that point. Thanks
-
Murali Suriar almost 15 years+1. Just to add, anyone looking for more in depth understanding should read one of W. Richard Stevens many excellent books on the subject. "TCP/IP Illustrated, v1" and "UNIX Network Programming" serve as excellent tutorials and references.
-
Alnitak almost 15 years@splattne - order of message receipt is guarantee, not transmission
-
splattne almost 15 years@Alnitak: of course, you're right. I meant the receipt is guaranteed in the transmission order. I'll update to make that clear.
-
pQd almost 15 yearsyou are not entirely right. dns uses also tcp/53, not so often but still [ eg. for zone transfer ].
-
hayalci almost 15 yearsyes. Also, it is entirely valid to send regular dns queries over tcp. I changed the example to TFTP, which exclusively uses UDP.
-
Keithius almost 15 yearsThat is a very good "CEO level" explanation - I like it!
-
Nicolas Dorier almost 15 yearsI'm not a CEO, but why nobody told me this earlier ! :p
-
shylent almost 15 yearsThis is brilliant!
-
user1047402 almost 15 yearsWrong. The IP protocol is not reliable and data loss can occur. TCP makes an effort to ensure that sent data (which is split into small parts and sent via IP packets) will make it to the recipient (by eventually resending small parts if required).
-
Anis Ghee over 14 years@ Anonymous - You basically restated what I said above. How am I wrong?
-
Lee B about 14 yearsI think you'll both find that IP is the underlying framework (stack) and the basic communication protocol that both UDP and TCP use. You can't actually communicate useful data over the internet using JUST IP, as far as I know -- even basic pings etc. use a protocol on top of IP (the other main one; ICMP).