Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

11 Answers 11

up vote 45 down vote accepted

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.

share|improve this answer
2  
That 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 –  Guy May 16 '09 at 20:38
15  
+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. –  Murali Suriar May 16 '09 at 23:47

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 strcuture:

alt text

An this the UDP frame structure, much simpler:

alt text

Typical protocols which use TCP are HTTP, FTP and SMTP. Examples of protocols using UDP are DNS and DHCP.

share|improve this answer
1  
+1 - this helped me, and I know a bit about this. Thanks! –  tomjedrz May 16 '09 at 21:13
    
@splattne - order of message receipt is guarantee, not transmission –  Alnitak May 17 '09 at 6:46
    
@Alnitak: of course, you're right. I meant the receipt is guaranteed in the transmission order. I'll update to make that clear. –  splattne May 17 '09 at 7:19
    
Great answer splattne - thanks. –  Guy Jul 7 '09 at 23:19
1  
these images have died. –  strugee Apr 20 at 23:04

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.

share|improve this answer
1  
That is a very good "CEO level" explanation - I like it! –  Keithius Jun 4 '09 at 12:36
    
I'm not a CEO, but why nobody told me this earlier ! :p –  Nicolas Dorier Jun 20 '09 at 8:20
    
This is brilliant! –  shylent Jun 20 '09 at 11:57
2  
In case of TCP, you don't pick up the missed paper: instead, 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. –  nixnotwin May 8 '13 at 6:26

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.

share|improve this answer
    
Thanks Brian - helpful. –  Guy May 16 '09 at 20:38

As well as differences mentioned so far TCP also provides

flow control. Using a window mechanism the recipient limits how many octets (bytes) it wants to receive from the sender before the sender must wait for permission to send more data. The size of the window can be changed by the receiver as the conversation proceeds. This allows a receiver to 'throttle' the rate of data coming from a sender: UDP does not allow this and all a receiver can do is throw away data it cannot buffer or process on arrival.

some more technical facilities such as non-contiguous acknowledgement (uses selective acknowledgements to allow the sender to retransmit just a missing frame rather than the missing frame and subsequent ones that the receiver already has but could not acknowledge in the usual way), congestion handling and adaptive retransmission (adjusting the timer that decides how soon to resend a transmitted frame when an acknowledgement is not received). These are not relevant to UDP but help TCP to provide extra services over UDP without greatly increasing the network load for the same payload volume.

share|improve this answer

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.

share|improve this answer
    
you are not entirely right. dns uses also tcp/53, not so often but still [ eg. for zone transfer ]. –  pQd May 24 '09 at 10:40
    
yes. Also, it is entirely valid to send regular dns queries over tcp. I changed the example to TFTP, which exclusively uses UDP. –  hayalci May 25 '09 at 11:30
    
actually, you changed it to TFPT rather than TFTP... Unfortunately I have as yet too little rep to fix it for you. –  Peter Hansen Apr 19 '10 at 11:31
    
@peter-hansen thanks, corrected the error. –  hayalci Apr 25 '10 at 23:23

TCP makes an effort to ensure the data made it to the recipient; UDP does not.

share|improve this answer
    
Wrong. 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). –  Anonymous Jul 13 '09 at 18:57
    
@ Anonymous - You basically restated what I said above. How am I wrong? –  J. Polfer Sep 22 '09 at 14:07
    
I 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). –  Lee B Apr 25 '10 at 23:34

Never mind all the answers explaining one vs. the the other. For the purposes of setting up a port-forward, the rules are pretty simple: you need to choose TCP, unless the docs for the protocol/application you're using say to use UDP. Usually the ones that use UDP are related to streaming, or very simple things that favour lightweight protocols/speed over reliability (particularly DNS and NTP). Note that there are other protocols besides TCP and UDP too -- ICMP, tunneling protocols, etc.

share|improve this answer

The more wellknowned a protocol is, the more routers on the path intervenes with the protocol. Send tcp packets, and there's a big probability the received packets are highly mangled. UDP is usually not that mangled, and the other 254 IP paylod protocols are usually completely untouched.

share|improve this answer

User Datagram Protocol (UDP)

It is part of the base protocols of the Internet Protocol Suite. Programs on networked computers can send short messages sometimes called as datagrams. UDP does not guarantee any reliability( it happens datagram may arrive out of order, are duplicated, or are missing without any notice). The fact that no checking whether all packets are actually delivered is made, UDP proves to be faster and more efficient, for applications that do not need guaranteed delivery. UDP find its uses in such situations:

Time-sensitive applications. The problems due to delayed packets are avoided

It is also useful for servers that answer small queries from huge numbers of clients. UDP supports packet broadcast (conveys to all on local network) and multicasting (conveys to all subscribers).

Transmission Control Protocol (TCP)

It is often referred to as TCP/IP due to the importance of this protocol in the Internet Protocol Suite. TCP operates at a higher level, concerned only with the two end systems, (e.g. between web browser and a web server). TCP provides reliable, sequential delivery of a stream of data from one program on one computer to another program on another computer. Common uses of TCP regroup e-mailing support and file transfer and Web applications. Among its management tasks, TCP controls message size, the rate at which messages are exchanged, and network traffic congestion. As for IP, it handles lower-level transmissions from computer to computer as a message transferred across the Internet.

share|improve this answer

Well both are network protocols built on top of something called Internet Protocol or IP. The Internet Protocol is the major protocol of the Internet and is for example what allows us to browse the web. That's why you will often see these protocols written as TCP/IP or UDP/IP. They are actually core protocols of the internet.

In both cases your data is split up into chunks called packets. TCP ensures that these packets get to their destination and in the correct order and without any errors.

UDP on other hand will not ensure that a data packet will even arrive. It will only ensure that if it does arrive it will be without error.

Typically, other protocols will run on top of TCP and UDP and are known as application protocols because they operate within an application and not within the operating system. Your web browse uses HTTP protocol which will use the TCP protocol.

Domain name lookups or DNS queries often use UDP but can also use TCP. A number of streaming services use UDP beneath another application protocols.

I hope that answers your question in simpler english.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.